简体   繁体   中英

Selecting all rows for 3 tables using INNER JOIN in JPQL

I have a problem in selecting all rows within 3 tables in JPQL. I want to return it as a 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? Or Should I use Query only not TypedQuery?

Thank you.

Thanks to this link : INNER JOIN IN JPQL

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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