繁体   English   中英

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

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

在选择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? 还是应该仅使用查询而不使用TypedQuery?

谢谢。

感谢此链接: JPQL的INNER JOIN

例如:

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

h.vehicleID为v

它仅表明我需要首先引用左表中的外键并为其提供别名,以便我可以轻松地调用它并在ON中使用它

暂无
暂无

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

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