我正在使用带有Hibernate的JPA的Java Spring Project。有两个实体“主题”和“导师”。在“主题”类中,有一个ManyToMany列表作为“Tutor”的引用,如下面的代码片段所示:
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”实体没有向后引用“主题”。
我想要获得的是获取一个主题列表,其中包含所有主题,其中包含preferedTutors列表属性中的某个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
显然是“。”在生成的语句末尾的表达式“(。not not null)”中无法处理。这是一个已知的错误还是我必须改变我的JPA查询?
谢谢,Max
答案 0 :(得分:0)
尝试使用@JoinTable定义您的ManyToMany,如下所示:
public class Topic implements Serializable {
...
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@JoinTable(name="preferredtutors", joinColumns={@JoinColumn(name="topic_id")}, inverseJoinColumns={@JoinColumn(name="tutor_id")})
private List<Tutor> preferedTutors = new ArrayList<Tutor>();
....
使用@JoinTable注释,您可以定义表和列引用。