SpringData JPA用于IN关键字的hibernate集合

时间:2013-06-19 07:27:49

标签: spring-data-jpa

我正在尝试使用SpringData存储库中的客户查询来获取记录。我想使用IN关键字根据嵌套对象的id获取对象。以下是我的回购课程。

public interface BusinessRepository  extends JpaRepository<Business, Long> {
    @Query("SELECT t from Business t where t.address.addressid  IN  ? AND (t.businessType.text like ? or t.businessname like ?)")
    ArrayList<Business> findAllByAddressAddressidInAndBusinessTypeTextLikeOrBusinessnameLike(ArrayList<Long> ids,  String businessType, String businessName);

我的控制台显示以下文字。

Hibernate: select business0_.businessid as businessid0_, business0_.address_addressid as address9_0_, business0_.begdate as begdate0_, business0_.businessType_id as busines10_0_, business0_.businessname as business3_0_, business0_.contracttypeid as contract4_0_, business0_.multiuser as multiuser0_, business0_.statusid as statusid0_, business0_.urlreservos as urlreser7_0_, business0_.website as website0_, business0_1_.homePhone as homePhone2_, business0_1_.mobilePhone as mobilePh2_2_, business0_1_.primaryPhone as primaryP3_2_, business0_2_.businessdesc as business1_1_, business0_2_.pagetitle as pagetitle1_, business0_2_.tag as tag1_, business0_3_.userid as userid3_ from tbu1200 business0_ left outer join tbu1208 business0_1_ on business0_.businessid=business0_1_.id left outer join tbu1207 business0_2_ on business0_.businessid=business0_2_.id left outer join tbu1204 business0_3_ on business0_.businessid=business0_3_.businessid cross join vt1001 businessty1_ where business0_.businessType_id=businessty1_.id and (business0_.address_addressid in (?)) and (businessty1_.text like ? or business0_.businessname like ?)
TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [1, 2]

对我来说似乎是对的。我正在接受例外。

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long
at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:36)
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:57)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92)

我的问题是,为什么在使用IN语句时期望Long。它应该期待一个集合。如果这不支持,那我该怎么办?

1 个答案:

答案 0 :(得分:1)

为什么要在此处添加@Query注释?

如果不手动指定查询,它应该按预期工作。

List<Company> findDistinctByUsers_loginIn(List<String> users)

我已经检查过,这样的代码完成无一例外。唯一的问题是SpringData正在创建笛卡尔,你需要添加distinct以使其正常工作。