简体   繁体   中英

Spring Data JPA check if object exits where elementcollecion is equal to list

I have created element collection that contains ids to another table

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "participantchatchannel", joinColumns = 
@JoinColumn(name = "channelId"))
@Column(name = "participantId")
private List<Long> participantsIdList;

I can see that the list contains the values I want it to contain. Now I want to check if there is an object that contains exactly the same Ids I am providing.

I have tried expanding CrudRepository like so:

boolean existsByParticipantsIdListEquals(List<Long> participantIdList);

But I keep getting error:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ','.

What am I doing wrong and why (possibly I don't quite understand what sql is generated by this).

Edit:

After enabling logs I can see following lines in console:

2018-08-10 07:27:14.997 DEBUG 8880 --- [nio-8080-exec-2] org.hibernate.SQL                        : select TOP(?) chatchanne0_.id as col_0_0_ from chatchannel chatchanne0_ left outer join participantchatchannel participan1_ on chatchanne0_.id=participan1_.channel_id where participan1_.participant_id=(? , ?)
2018-08-10 07:27:15.001 TRACE 8880 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [2]
2018-08-10 07:27:15.002 TRACE 8880 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BIGINT] - [3]
2018-08-10 07:27:15.005  WARN 8880 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 102, SQLState: S0001
2018-08-10 07:27:15.005 ERROR 8880 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Incorrect syntax near ','.

Your method signature is wrong. You cannot use Equals with a collection as argument. That's why Hibernate generate an invalid where clause:

where participan1_.participant_id=(? , ?)

You have to use In. So your query should look like:

boolean existsByParticipantsIdListIn(List<Long> participantIdList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM