Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA - left join 2 tables without association

I have 2 tables/entities with no association. I can go for cross joins in JPA

FROM A as a, B as b WHERE b.col1=a.col1

How Can I perform a left-join?

I want all values from A and fill them up with B values where possible and leave NULL where there is no B.

Does not work:

FROM A as a LEFT JOIN B as b WHERE b.col1=a.col1

Path expected for join!

Invalid path: 'b.col1'

like image 394
Dimitri Dewaele Avatar asked Dec 09 '13 14:12

Dimitri Dewaele


2 Answers

Apparently you can now do this as of Hibernate 5.1

like image 108
nevster Avatar answered Sep 19 '22 12:09

nevster


You cannot do that in JPA, as long as there is no relationship between the entities.

Solutions:

  1. Use Native queries.
  2. Add a relationship between them (eventually an indirect lazy one). With indirect I mean something like : A knows PseudoEntity, PseudoEntity knows B (but the relationship owner is entity B), B knows PseudoEntity.
like image 9
V G Avatar answered Nov 20 '22 22:11

V G