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

时间:2015-12-15 00:39:59

标签: spring-boot spring-data spring-data-jpa spring-data-commons

升级到spring boot版本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$1.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。

谢谢!

1 个答案:

答案 0 :(得分:0)

如果这有用,那就是一个错误。 findByActiveTrue_productInfoActiveTrue(…)不是有效的查询方法。根据您的域类型,该方法包含两个谓词:active = trueproductInfo.active = true。那些谓词需要使用关键字连接,在我看来,我认为这可能是AndfindByActiveTrueAndProductInfoActiveTrue(…)不仅读得更好,而且也应该有效。

为了确保您理解错误消息,请发生以下情况:

  1. 我们删除前缀,直到第一个By - &gt; ActiveTrue_productInfoActiveTrue
  2. 我们剥离了关键字 - &gt; ActiveTrue_productInfoActive
  3. 我们现在尝试从完全匹配开始在域对象中查找属性,从右侧缩短它。
  4. 我们最终找到了剩余的Active左侧的True_productInfoActive左侧。
  5. 我们使用已解析的属性类型(Boolean)重复从3开始的过程,最终无法找到某些内容,true是最后一个尝试过的细分。
  6. 一般来说,我有兴趣听听是什么让你首先使用下划线,因为下划线本质上是一个&#34;遍历嵌套属性到右边&#34;命令解析器,这显然不是你的方法的意图。