0

升級到彈簧引導版本1.3.0.RELEASE後,嘗試啓動時發現以下失敗。彈簧引導1.3.0升級和JPA自定義方法不起作用

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。

謝謝!

+0

調試進一步,它似乎應該剝離「真/假」的代碼沒有這樣做。令人費解的是,沒有人報告過這個問題,因爲這會影響每個有布爾回購方法的人。它從方法的末尾剝去「真」,但不從其他第一次出現。這是發生了什麼 - ActiveTrue_productInfoActiveTrue => ActiveTrue_productInfoActive相反,它應該導致此Active_productInfoActive – slad

回答

0

如果這有效,那是一個錯誤。 findByActiveTrue_productInfoActiveTrue(…)不是有效的查詢方法。根據您的域名類型,該方法由兩個謂詞組成:active = trueproductInfo.active = true。那些謂詞需要連接使用關鍵字,在你的情況下,我認爲可能是AndfindByActiveTrueAndProductInfoActiveTrue(…)不僅讀得更好,它也應該工作。

要確保你明白這一點,發生了什麼錯誤消息:

  1. 我們直到第By剝離前綴 - >ActiveTrue_productInfoActiveTrue
  2. 我們的關鍵詞條 - >ActiveTrue_productInfoActive
  3. 我們現在嘗試從完整匹配開始在域對象中查找屬性,並將其從右側縮短。
  4. 我們最終找到最左邊的Active,其餘尾部爲True_productInfoActive
  5. 我們重複從3開始的已解析屬性類型(Boolean)並最終無法找到某個內容,true是最後一個嘗試的段。

一般來說,我會有興趣聽到首先使用下劃線是什麼原因,因爲下劃線本質上是一個「遍歷嵌套屬性到右邊」的命令給解析器,顯然不能你的方法的意圖。

+0

感謝您的答覆。你的建議解決了這個問題。我之前所做的與早期版本的spring-data-common一起工作得很好,這很奇怪。當我錯誤地解釋這一部分時,我很困惑 - http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions – slad

相關問題