SpringData JPA hibernate collection for IN keyword

有些话、适合烂在心里 提交于 2021-01-27 11:01:00

问题


I am trying to fetch records using custome query in SpringData repository. I want to use IN keyword to fetch objects according to ids of a nested object. Following is my repo class.

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);

My console shows following text.

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]

It seems correct to me. I am getting following exception.

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)

My question is that why it is expecting Long while I have used IN statement. It should expect a Collection. If this is something not supported then what should I do?


回答1:


Why are you adding @Query annotation here?

Without specifing query manually it should works as expected.

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

I've checked and such code finishes without exception. The only problem is that SpringData is creating cartesian, and you need to add distinct to make it working properly.



来源:https://stackoverflow.com/questions/17185266/springdata-jpa-hibernate-collection-for-in-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!