简体   繁体   中英

how to create jpql with left outer join

I have for example 2 entities.
A entity (Mysql Table name="A")
B entity (Mysql Table name="B")
I want fetch data from A table where it is not in the B table.
I wrote sql and it is working.
SELECT * FROM A LEFT OUTER JOIN B ON A.id = B.a_id WHERE B.id IS null

How to realize it with JPQL?

I believe the following should work:

SELECT A FROM A a LEFT JOIN a.B b WHERE b.id = null;

This should join A with B on id leaving null where A can not match with B. Then it selects the rows from A where b.id is null. Seems like what you're looking for. Also checkout the following SOs: How to make a JPA query with LEFT OUTER JOIN And as done in the following: How to create a JPA query with LEFT OUTER JOIN

Let me know whether it worked out.

You may use this command

SELECT * FROM A WHERE A.id NOT IN (SELECT B.id FROM B)

If you wan to use only join let me know

If you have some kind of following structure:

Class EntityA
--------
long Id
Set<EntityB> Bs

Class EntityB
-------
long Id
EntityA A

I think following should work

SELECT a FROM EntityA a WHERE a.Bs IS EMPTY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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