java - JPA/JPQL JOIN Subselect/Subquery -
i have trouble translate easy sql statement jpql, because of used subquery, not supported jpql.
can give me hint, how achieve same result jpql or jpa2 criteria api?
given (simplified fake data demonstrate problem):
create table person (id integer, name text); create table phone (id integer, person_id integer, type text, number text); insert person values (1, "john"); insert person values (2, "mike"); insert person values (3, "paul"); insert person values (4, "walter"); insert phone values (1, 1, "mobile", "+49-123-11111"); insert phone values (2, 1, "home" , "+49-123-22222"); insert phone values (3, 2, "work" , "+49-123-33333"); insert phone values (4, 4, "mobile", "+49-123-44444"); -- select person , mobile number if possible -- query has translated jpql select person.name, mobile.number person left join ( select * phone type = "mobile" ) mobile on person.id = mobile.person_id;
expected result:
| name | number | |--------|---------------| | john | +49-123-11111 | | mike | | | paul | | | walter | +49-123-44444 |
java:
class person { string name; list<phone> phones; } class phone { string type; string number; }
jpql (not working expected :-( ):
select person.name, phone.number person person left join person.phones phone phone.type = "mobile"
select person.name, phone.number person person left join person.phones phone on phone.type = 'mobile'
you can replace on
keyword hibernate specific with
:
select person.name, phone.number person person left join person.phones phone phone.type = 'mobile'
Comments
Post a Comment