我正在使用 JPA 和 Hibernate 处理 Java Spring 项目。有两个实体“主题”和“导师”。在“主题”类中,有一个多对多列表作为对“导师”的引用,您可以在以下代码段中看到:
public class Topic implements Serializable {
...
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@JoinColumn(name = "pretutor_id")
@IndexColumn(name = "id")
private List<Tutor> preferedTutors = new ArrayList<Tutor>();
....
“Tutor”实体没有向后引用“Topic”。
我想要实现的是获得一个包含所有主题的主题列表,这些主题在它们的preferedTutors List 属性中包含某个Tutor对象。为此,我尝试了
"SELECT t FROM Topic t WHERE :tutor MEMBER OF t.preferedTutors"
其中参数 ":tutor" 是一个 "Tutor" 对象。代码编译正确,但在测试中执行时我不幸收到以下错误:
Caused by: java.sql.SQLException: Unexpected token: . in statement [select topic0_.id as id4_0_, diplomate2_.id as id1_1_, tutor4_.id as id1_2_, topicdescr5_.id as id6_3_, topic0_.descriptionfile_id as descript3_4_0_, topic0_.procedure_id as procedure4_4_0_, topic0_.diplomate_id as diplomate5_4_0_, topic0_.title as title4_0_, topic0_.tutor_id as tutor6_4_0_, diplomate2_.accountName as accountN2_1_1_, diplomate2_.description as descript3_1_1_, diplomate2_.procedure_id as procedure8_1_1_, diplomate2_.email as email1_1_, diplomate2_.firstname as firstname1_1_, diplomate2_.lastname as lastname1_1_, diplomate2_.title as title1_1_, prospectiv1_.Topic_id as Topic1_4_0__, prospectiv1_.prospectiveDiplomates_id as prospect2_0__, prospectiv1_.id as id0__, tutor4_.accountName as accountN2_1_2_, tutor4_.description as descript3_1_2_, tutor4_.procedure_id as procedure8_1_2_, tutor4_.email as email1_2_, tutor4_.firstname as firstname1_2_, tutor4_.lastname as lastname1_2_, tutor4_.title as title1_2_, tutor4_.contingent as contingent5_2_, tutor4_.createdAt as createdAt5_2_, tutor4_.isAdmin as isAdmin5_2_, preferedtu3_.Topic_id as Topic1_4_1__, preferedtu3_.preferedTutors_id as prefered2_1__, preferedtu3_.id as id1__, topicdescr5_.file as file6_3_, topicdescr5_.fileName as fileName6_3_, topicdescr5_.fileType as fileType6_3_, topicdescr5_.uploadDate as uploadDate6_3_ from Topic topic0_ left outer join Topic_Diplomate prospectiv1_ on topic0_.id=prospectiv1_.Topic_id left outer join Diplomate diplomate2_ on prospectiv1_.prospectiveDiplomates_id=diplomate2_.id left outer join Topic_Tutor preferedtu3_ on topic0_.id=preferedtu3_.Topic_id left outer join Tutor tutor4_ on preferedtu3_.preferedTutors_id=tutor4_.id left outer join TopicDescription topicdescr5_ on topic0_.descriptionfile_id=topicdescr5_.id cross join Topic_Tutor preferedtu6_, Tutor tutor7_ where topic0_.id=preferedtu6_.Topic_id and preferedtu6_.preferedTutors_id=tutor7_.id and (topic0_.diplomate_id is not null) and (. is not null)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:248)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:302)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1616)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
... 58 more
显然是“。” 无法处理生成语句末尾的表达式“(. is not null)”。这是一个已知的错误还是我必须更改我的 JPA 查询?
谢谢,马克斯