我有两个像A和B这样的实体:
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
}
我有一个spring数据存储库,以便:
@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 :(得分:23)
似乎我们必须对查询中的所有参数使用SPeL,而不是混合jpa方式和SPel::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);
答案 1 :(得分:-1)
这是错误的:
在findByBAndDate中(@Param(值="日期")日期日期, @Param(值=" b")B b )
日期是一个参数,所以得到 @Param(值="日期")但是b不是参数它是一个对象所以得到它作为 @ModelAttribute( " b")B b 或其他任何取决于您的要求。