0

升级到 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。

谢谢!

4

1 回答 1

0

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

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

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

一般来说,我很想知道是什么让你首先使用下划线,因为下划线本质上是解析器的“将嵌套属性向右遍历”命令,这显然不是你的方法的意图。

于 2015-12-17T20:16:32.070 回答