【问题标题】:How to convert the below SQL to JPQL?如何将以下 SQL 转换为 JPQL?
【发布时间】:2014-03-08 23:10:15
【问题描述】:

如何将下面的 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)

【问题讨论】:

  • 去掉“NameTest”和“LEFT JOIN”之间的“,”

标签: java sql oracle jpql


【解决方案1】:

试试这个:

"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";

【讨论】:

  • 我收到以下错误“遇到”在字符 61 处遇到“JOIN 位置 AS”,但预期为:[“.”、“AS”、“FETCH”、“INNER”、“JOIN”, "左", ]。"
  • @stacktome 我已经更新了我的答案,所以你可以尝试新的变种
  • 我仍然收到相同的错误“在字符 55 处遇到“LEFT JOIN Location ON”,但预期为:[“.”、“FETCH”、“INNER”、“JOIN”、“LEFT” ", "外部", ]。"在解析 JPQL ""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";"
  • @stacktome 检查另一个答案,可能是这样
【解决方案2】:

你能试试这个代码吗:

    "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 中得到支持,对于休眠支持,请查看以下答案: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

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 2014-08-02
    • 2018-03-16
    • 2018-10-29
    • 1970-01-01
    • 2022-12-05
    • 2011-09-29
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多