How to create a JPA query with LEFT OUTER JOIN

前端 未结 3 1227
攒了一身酷
攒了一身酷 2020-12-03 10:16

I am starting to learn JPA, and have implemented an example with JPA query, based on the following native SQL that I tested in SQL Server:

SELECT f.StudentID         


        
相关标签:
3条回答
  • 2020-12-03 10:46

    If you have entities A and B without any relation between them and there is strictly 0 or 1 B for each A, you could do:

    select a, (select b from B b where b.joinProperty = a.joinProperty) from A a
    

    This would give you an Object[]{a,b} for a single result or List<Object[]{a,b}> for multiple results.

    0 讨论(0)
  • 2020-12-03 10:54

    Write this;

     SELECT f from Student f LEFT JOIN f.classTbls s WHERE s.ClassName = 'abc'
    

    Because your Student entity has One To Many relationship with ClassTbl entity.

    0 讨论(0)
  • Normally the ON clause comes from the mapping's join columns, but the JPA 2.1 draft allows for additional conditions in a new ON clause.

    See,

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

    0 讨论(0)
提交回复
热议问题