JPA Criteria API: Multiple condition on LEFT JOIN

拜拜、爱过 提交于 2019-11-28 10:20:27
James

JPA always joins by the mapping join columns, only. JPA 2.0 does not support an ON clause, but the 2.1 draft does have this support.

EclipseLink 2.4 has support for an ON clause,

See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#ON

It is also possible through the Criteria API, using the EclipseLink native Expression API that provides on clause support,

See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Criteria#JpaCriteriaBuilder_and_EclipseLink_Extensions

And Hibernate's Criteria API version 3.6 supports it too:

    Criteria c = session.createCriteria(Tuple.class);

    c.createAlias("demands", "d")
    .createAlias("d.metadata", "m", 
        Criteria.LEFT_JOIN, Restrictions.eq("m.type", "AFFECTATION_KEY"));

Or you may want Restrictions.eqProperty() for that last bit. Note the under-appreciated version of createAlias() with the fourth parameter. Here is a quote from the documentation

Parameters:

  • associationPath - A dot-seperated property path
  • alias - The alias to assign to the joined association (for later reference).
  • joinType - The type of join to use.
  • withClause - The criteria to be added to the join condition (ON clause)

Hibernate API 3.6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!