JPA条件:使用custom on子句连接表

时间:2016-12-30 10:45:39

标签: jpa criteria openjpa

我是JPA的新手,我想使用API​​条件创建此查询:

SELECT company.*
FROM company
LEFT OUTER JOIN address ON company.id = address.company_id AND address.postal_code = '54000'
ORDER BY company.id;

在搜索和反思之后,我尝试了这段代码,这对我来说是合乎逻辑的:

...
CriteriaQuery<Company> query = criteriaBuilder.createQuery(Company.class)
Root<Company> defCompany = query.from(Company.class);
Join joinAddresses = defCompany.join("addresses", JoinType.LEFT);
joinAddresses.on(criteriaBuilder.equal(joinAddresses.get("postalCode"), "54000"));
query.orderBy(criteriaBuilder.asc(defCompany.get("id")));
List<Company> companies = entityManager.createQuer(query)
    .getResultList();
...

但是OpenJPA抛出了这个错误:

ERROR OpenEJB- EjbTransactionUtil.handleSystemException: org.apache.openjpa.persistence.criteria.Joins$List.on(Ljavax/persistence/criteria/Expression;)Ljavax/persistence/criteria/Join;
    java.lang.AbstractMethodError: org.apache.openjpa.persistence.criteria.Joins$List.on(Ljavax/persistence/criteria/Expression;)Ljavax/persistence/criteria/Join;

我希望以前有人试图这样做,我想可能但是我找不到关于此的线索或问题。

1 个答案:

答案 0 :(得分:1)

JOIN ON是JPA 2.1的一部分,OpenJPA没有实现JPA 2.1。

使用实现JPA 2.1的JPA提供程序(例如DataNucleus JPA,EclipseLink,Hibernate),或者不要在JOIN中使用ON子句(或者将ON放在WHERE子句中......不是很清楚)< / p>