Spring 引导 1.3.0 升级和 JPA 自定义方法不起作用

Spring boot 1.3.0 upgrade and JPA custom method not working

升级到 spring 引导版本 1.3 后。0.RELEASE,我在尝试启动时看到以下错误。

Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'productRepository': Invocation of init
method failed; nested exception is
org.springframework.data.mapping.PropertyReferenceException: No
property true found for type boolean! Traversed path: Product.active.
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
        ... 64 more Caused by: org.springframework.data.mapping.PropertyReferenceException: No property true found for type boolean! Traversed path: Product.active.
        at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)

这是导致问题的相关代码片段。没有对任何存储库或数据模型进行更改。

public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findByActiveTrue_productInfoActiveTrue();
}

@Entity
@DiscriminatorValue(value = "0")
public class Product extends BaseProduct {

    public boolean isSpecial() {
        return special;
    }

    public void setSpecial(boolean special) {
        this.special = special;
    }
}

@Entity(name = "products")
@DiscriminatorColumn(name = "matchtype", discriminatorType = DiscriminatorType.INTEGER)
public abstract class BaseProduct implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "isspecial")
    protected boolean special;

    @Id
    @GeneratedValue
    @Column(name = "productid")
    private Long productId;

    @Column(name = "active")
    private boolean active;

    @OneToMany(mappedBy = "productId", fetch = FetchType.EAGER)
    private Set<ProductInfo> productInfo;

    @Entity(name = "products_ranges")
    public static class ProductInfo {

        @Id
        @Column(name = "prodrangeid")
        private Long id;

        @Column(name = "product_id")
        private Long productId;

        @Column(name = "active")
        private boolean active;
    }

如能就此问题提供任何帮助,我们将不胜感激。我试着回到旧版本的 spring-data-common 但这不适用于新的 spring-data-jpa.

谢谢!

如果这曾经奏效,那就是一个错误。 findByActiveTrue_productInfoActiveTrue(…) 不是有效的查询方法。根据您的域类型,该方法由两个谓词组成:active = trueproductInfo.active = true。那些谓词需要使用关键字连接,在你的情况下我假设可能是 AndfindByActiveTrueAndProductInfoActiveTrue(…) 不仅读起来更好,而且还应该有效。

为了确保您理解错误消息,这是发生了什么:

  1. 我们去掉前缀直到第一个 By -> ActiveTrue_productInfoActiveTrue
  2. 我们去掉关键字 -> ActiveTrue_productInfoActive
  3. 我们现在尝试在域 object 中查找属性,从完整匹配开始,从右边开始缩短。
  4. 我们最终找到了最左边的 Active,剩下的尾巴是 True_productInfoActive
  5. 我们使用已解析的 属性 类型 (Boolean) 从 3 开始重复该过程,但最终找不到任何东西,true 是最后尝试的片段。

一般来说,我很想知道是什么让您首先使用下划线,因为下划线本质上是对解析器的 "traverse the nested property to right" 命令,这显然不是您的意图方法。