2016-03-15 80 views
3

我有兩個實體,例如A和B這樣的:沒有參數綁定發現名稱(春季數據JPA)

class B { 
    private Integer id; 
    private String field1; 
    private String field2; 
    // getters and setters 
} 

class A { 
    private Date date; 
    //one to one mapping is there between A and B 
    private B b; 
    //getters and setters 
} 

我有一個春天的數據存儲庫這樣的:

@Query("from A a where a.date= :date and a.b.id =:#{#b.id}") 
     A findByBAndDate(@Param(value = "date") Date date,@Param(value = "b") B b); 

但我我得到了例外,no parameter binding found for name b!

但是,如果我修改上面的查詢爲:

@Query("from A a where a.b.id =:#{#b.id}") 
A findByB(@Param(value = "b") B b); 

一切工作正常。這有什麼問題。

回答

0

這是錯誤的:

在findByBAndDate(@Param(值= 「日期」)日期日期,@Param(值= 「B」)B B

日期是一個參數,以便得到@參數(值=「日期」)但b不是參數它是一個對象,所以得到這個作爲@ModelAttribute(「b」)B b或任何其他取決於您的要求。

+1

它的正常工作與 @Query(「從一個地方阿比德=#{# b.id}「) findByB(@Param(value =」b「)B b);由於spring爲spring數據提供了SPEL支持,因此請仔細閱讀該問題。 – user1157635

11

似乎我們必須使用SPeL查詢所有參數,而不是混合jpa方式和speel::date混合:#{#b.id}

試試這個,我沒有測試過,我不知道它會如何表現了Date

@Query("from A a where a.date= :#{#date} and a.b.id =:#{#b.id}") 
     A findByBAndDate(@Param(value = "date") Date date,@Param(value = "b") B b);