Imperative Left Outer Join in Hibernate

1. Urgent left outer join: Left join fetch

If this retrieval strategy is adopted, the collection returned by the list() method stores the reference of entity objects, and the objects associated with each entity object will also be initialized , so the entity objects of all associated objects will also be stored.

E.g:

There are the following two entity classes:

  1. Employee
  2. Department

    There is an attribute of type Department in Employee (there is a many-to-one relationship), if we get all the data in the employee table: FROM Employee , we can get all the Employee entity objects, but the associated Department has not been initialized , such as To use its Department object after the session is closed will throw a lazy loading exception.


If you need to initialize the associated Department object when you get the Employee object, you need to use an eager left outer join (or directly add lazy="false" to the mapping relationship to turn off lazy loading):


    String hql = "FROM Employee e LEFT JOIN FETCH e.department";

Using LEFT JOIN FETCH through the above hql When obtaining entity objects, the objects associated with each entity object will also be initialized, so the entity objects of all associated objects will also be stored.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324538557&siteId=291194637