简体   繁体   English

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

[英]Selecting all rows for 3 tables using INNER JOIN in JPQL

I have a problem in selecting all rows within 3 tables in JPQL. 在选择JPQL的3个表中的所有行时遇到问题。 I want to return it as a Collection<Object> . 我想将其作为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;
}

The error showing is : 显示的错误是:

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.

Is it the query? 是查询吗? Should I use createQuery or createNativeQuery? 我应该使用createQuery还是createNativeQuery? Or Should I use Query only not TypedQuery? 还是应该仅使用查询而不使用TypedQuery?

Thank you. 谢谢。

Thanks to this link : INNER JOIN IN JPQL 感谢此链接: JPQL的INNER JOIN

Like for example: 例如:

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

h.vehicleID as v h.vehicleID为v

it only tells that i need to reference first the foreign key from the left table and give it an alias so that i may easily call it and use it in ON 它仅表明我需要首先引用左表中的外键并为其提供别名,以便我可以轻松地调用它并在ON中使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM