gpt4 book ai didi

java - 在 JPQL 中使用 INNER JOIN 选择 3 个表的所有行

转载 作者:行者123 更新时间:2023-11-29 08:48:49 25 4
gpt4 key购买 nike

我在选择 JPQL 中 3 个表中的所有行时遇到问题。我想将其作为 Collection<Object> 返回.

protected Collection<Object> getRecords(){
emf = Persistence.createEntityManagerFactory("MyPersistenceUnit");
em = emf.createEntityManager();

em.getTransaction().begin();

TypedQuery<Object> query = em.createQuery("SELECT * FROM Vehiclehistory As h INNER JOIN Vehicles As v ON h.vehicleID = v.vehicleID INNER JOIN Clients As c ON h.clientID = c.clientID",Object.class);
Collection<Object> list = query.getResultList();

em.getTransaction().commit();

return list;
}

错误显示是:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: An exception occurred while creating  a query in EntityManager: 
Exception Description: Syntax error parsing [SELECT * FROM Vehiclehistory As h INNER JOIN Vehicles As v ON h.vehicleID = v.vehicleID INNER JOIN Clients As c ON h.clientID = c.clientID].
[138, 138] A select statement must have a FROM clause.
[7, 7] The left expression is missing from the arithmetic expression.
[9, 138] The right expression is not an arithmetic expression.

是查询吗?我应该使用 createQuery 还是 createNativeQuery?或者我应该只使用 Query 而不是 TypedQuery?

谢谢。

最佳答案

感谢此链接:INNER JOIN IN JPQL

例如:

SELECT v.[vehicleinfo] FROM Vehiclehistory AS h INNER JOIN h.vehicleID AS v ON h.vehicleID = v.vehicleID

h.vehicleID as v

它只告诉我需要首先引用左表中的外键并给它一个别名,以便我可以轻松地调用它并在 ON 中使用它

关于java - 在 JPQL 中使用 INNER JOIN 选择 3 个表的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23775497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com