[英]JPA join query not working
I am trying to use JOIN with JPA. 我正在尝试将JOIN与JPA一起使用。 This is my JPA query:
这是我的JPA查询:
SELECT o FROM Assessment AS o
INNER JOIN AssessmentText at
WHERE o = at.assessment
AND at.localeCode = :localeCode
The relation from the Assessment
to AssessmentText
is OneToMany
. 从
Assessment
到AssessmentText
OneToMany
的关系是OneToMany
。
When I am executing this query I am getting: 当我执行此查询时,我得到:
org.apache.openjpa.persistence.ArgumentException:
Encountered "INNER JOIN AssessmentText at" at character 31,
but expected: [".", "FETCH", "INNER", "JOIN", "LEFT", <IDENTIFIER>].
I am using JPA implementation: OpenJPA 2.2.1 with MySQL database. 我正在使用JPA实现:带有MySQL数据库的OpenJPA 2.2.1。
Why do I get this error and how to solve this? 为什么会出现此错误以及如何解决?
假设您希望获取评估文本,则可以执行此操作(假设评估具有一个名为评估文本的成员):
SELECT o FROM Assessment AS o LEFT JOIN FETCH o.assessmentText WHERE o.assessmentText.localeCode = :localeCode
您在查询中缺少ON
应该是这样的
SELECT o FROM Assessment AS o INNER JOIN AssessmentText at ON o = at.assessment WHERE at.localeCode = :localeCode
You didn't mention the relationship from Assessment to AssessmentText was - it needs to be used in the join declaration. 您没有提到从Assessment到AssessmentText的关系-它需要在连接声明中使用。
The query you do have shows a relationship from AssessmentText to Assessment which can be used if you refactor the query slightly: 您确实拥有的查询显示了评估文本与评估之间的关系,如果您稍微重构查询,可以使用该关系:
SELECT at.assessment FROM AssessmentText at WHERE at.localeCode = :localeCode
Have in mind you should create your query in the following way: 请记住,您应该通过以下方式创建查询:
Query query = entityManager.createQuery(queryString);
Not em.createNativeQuery
(if you were doing it so). 不是
em.createNativeQuery
(如果您这样做的话)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.