Search code examples
hibernatejpaeclipselink

Is there eclipselink equivalent of Hibernate.initialize?


Basically i have a big Entity with many lazy loaded collections: I havent used eclipselink, but i know in Hibernate you can do: Hibernate.initialize(entity); And it will initialize the entity. Is there something like that with eclipselink? currently am doing this: entity.collection.size() to force initialization...but it will be nice if i dont have to do that for all the attributes.


Solution

  • There is less of a need for such a method - Entities loaded in EclipseLink maintain a connection to their session as long as it hasn't been serialized, so can be loaded out side of a transactional scope in most cases.

    JPA covers this with defining an EntityGraph and specifying it as a loadgraph. The provider then is responsible for traversing the object and ensuring everything within the entity is loaded per the given EntityGraph. This allows you finer grained control, and can load relationships deep within the entity.

    There are a few tutorials better than what I can show. Maybe see https://www.baeldung.com/jpa-entity-graph or https://www.javacodegeeks.com/2014/11/jpa-entity-graphs.html . Main difference from initialize is that you would need to extract the primary key from the entity and pass it with the loadgraph hint to a findById method for it to load things for you. ie:

    Map<String, Object> hints = new HashMap();
    hints.add("javax.persistence.loadgraph", entityManager.getEntityGraph(graphName));
    Entity loadedEntity = entityManager.findById(Entity.class, id, hints);