如何将以下SQL转换为JPQL?
SELECT *
FROM SSSRC.NAME_TEST nameTest
LEFT JOIN TTREFERENCE.LOCATION location
ON nameTest.place= location.location_name WHERE nameTest.gender = 'Male'
AND nameTest.age= '50'
AND nameTest.name IS NOT NULL
ORDER BY nameTest.sortby ,location.location_order;
我像这样转换
"SELECT nameTest FROM NameTest nameTest, LEFT JOIN Location location ON nameTest.place = location.location_name WHERE nameTest.age= '50' and nameTest.gender = 'Male' and nameTest.name IS NOT NULL ORDER BY nameTest.sortby ,location.location_order";
但是
它无法正常发出以下错误
Exception in thread "main" <openjpa-2.0.0-r422266:935683 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: "Encountered "location" at character 66, but expected: [",", "GROUP", "HAVING", "INNER", "JOIN", "LEFT", "ORDER", "WHERE", <EOF>]." while parsing JPQL "SELECT nameTest FROM PrintRequest nameTest, LEFT JOIN LOCATION location ON nameTest.place = location.location_name WHERE nameTest.age= '50' and nameTest.gender = 'Male' and nameTest.name IS NOT NULL ORDER BY nameTest.sortby ,location.location_order";. See nested stack trace for original parse error.
at org.apache.openjpa.kernel.jpql.JPQLParser.parse(JPQLParser.java:51)
at org.apache.openjpa.kernel.ExpressionStoreQuery.newCompilation(ExpressionStoreQuery.java:150)
at org.apache.openjpa.kernel.QueryImpl.newCompilation(QueryImpl.java:670)
at org.apache.openjpa.kernel.QueryImpl.compilationFromCache(QueryImpl.java:652)
at org.apache.openjpa.kernel.QueryImpl.compileForCompilation(QueryImpl.java:618)
at org.apache.openjpa.kernel.QueryImpl.compileForExecutor(QueryImpl.java:680)
at org.apache.openjpa.kernel.QueryImpl.compile(QueryImpl.java:587)
at org.apache.openjpa.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:985)
答案 0 :(得分:0)
试试这个:
"SELECT nameTest
FROM NameTest as nameTest
LEFT JOIN Location
ON nameTest.place = Location.location_name
WHERE nameTest.age = '50' and
nameTest.gender = 'Male' and
nameTest.name IS NOT NULL
ORDER BY nameTest.sortby, Location.location_order";
答案 1 :(得分:0)
你可以尝试这段代码:
"SELECT nameTest FROM NameTest nameTest
LEFT JOIN Location location ON nameTest.place = location.location_name
WHERE nameTest.age= '50' and nameTest.gender = 'Male' and nameTest.name IS NOT NULL
ORDER BY nameTest.sortby, location.location_order";
注意ON子句在JPA 2.1中定义并在EclipseLink 2.4中受支持,对于hibernate支持,请检查以下答案:https://stackoverflow.com/a/5220962/1047582
在我看来,你的JPA不支持ON子句。对于简单的LEFT JOIN,您需要更改您的实体。例如,NameTest类有一个Location元素:
public class NameTest{
@JoinColumn(name = "locationId", referencedColumnName = "location")
@ManyToOne()
Location location;
(...)
}
在这种情况下,JPQL看起来像:
SELECT nameTest FROM NameTest nameTest
LEFT JOIN nameTest.location location
WHERE nameTest.age= '50' and nameTest.gender = 'Male' and nameTest.name IS NOT NULL
ORDER BY nameTest.sortby, location.location_order