JPA | EclipseLink左外连接

时间:2014-09-26 11:47:15

标签: java sql jpa eclipselink jpql

只是尝试将SQL - Query转换为HQL / JPQL,但它无法正常工作,并且在语句中找不到任何错误。

// SQL - Statement (Gives all Entries from Entity1 joined with Entity2 if exist. 
// If Entity not exist the column has <Null> value )

SELECT e1.id, e2.id FROM Entity e1 LEFT OUTER JOIN Entity e2 
    ON e1.ID = e2.ENTITY1_ID AND e2.ID = '2' ORDER BY e1.ID DESC

Entity1{
...
    @OneToMany(cascade=CascadeType.ALL, mappedBy="entity1")
    private Set<Entity2> entities;
}

Entity2{
...
    @ManyToOne
    private Entity1 entity1;
}

在JPQL中翻译:

SELECT e1.id, e2.id FROM Entity1 e1 LEFT JOIN Entity2 e2 
    ON e1.id = e2.entity1.id AND e2.xyz.id = '2'

JPQL翻译:

SELECT t0.ID, t1.ID FROM Entity1 t0 LEFT OUTER JOIN Entity2 t1 
    ON ((t0.ID = t2.ID) AND (t1.XYZ_ID = ?)), Entity1 t2 WHERE (t2.ID = t1.Entity1_ID)

例外: javax.persistence.PersistenceException:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DatabaseException

内部异常:java.sql.SQLSyntaxErrorException:Column&#39; T2.ID&#39; FROM列表中的任何表都不包含任何表,它在连接规范中显示,并且不在相同或HAVING子句的范围内,并且不在GROUP BY列表中。如果这是一个CREATE或ALTER TABLE语句,那么&#39; T2.ID&#39;目标表中没有列。 错误代码:30000

有谁知道出了什么问题?

0 个答案:

没有答案