Tuesday, November 13, 2018

Join in JPA

 In JPA, you must pay attention to two kinds of joins.

join fetch

Example: “… Person p join fetch p.dogs …” to bring the dogs list eagerly even the dog list is annotated with the “lazy” attribute. If we do not include the join fetch and executed a command like “person.getDogs()“, another “trip” to the database would be required.

left join

To find a Person that has no dogs, use left join in JPA similar to SQL. If you want to do a query that brings a fetch dog collection and Persons that has or without dogs you can use “…Person p left join fetch p.dogs…”