Spring Data JPA检查对象是否在elementcollecion等于list

时间:2018-08-09 13:05:14

标签: java spring-boot spring-data-jpa jpql

我创建了包含另一个表ID的元素集合

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

我可以看到列表包含我想要包含的值。现在,我要检查是否存在包含与我提供的ID完全相同的ID的对象。

我曾尝试像这样扩展CrudRepository:

boolean existsByParticipantsIdListEquals(List<Long> participantIdList);

但是我总是收到错误消息:

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 ','.

我在做什么错,为什么(可能我不太了解由此生成的sql)。

编辑:

启用日志后,我可以在控制台中看到以下行:

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 ','.

1 个答案:

答案 0 :(得分:0)

您的方法签名错误。您不能将Equals与集合一起用作参数。 这就是Hibernate生成无效的where子句的原因:

where participan1_.participant_id=(? , ?)

您必须使用In。因此您的查询应如下所示:

boolean existsByParticipantsIdListIn(List<Long> participantIdList);