JPA equivalent command to Hibernate.initialize

2019-05-27 03:31发布

问题:

I have a Lazy collection that I want to initialize at will in my service/controller. At this moment I can do:

Hibernate.initialize( myEntity.getListOfThings() );

This command is hibernate dependent and does not make the implementation of JPA transparent. Is there a JPA elegant way of doing this?

回答1:

No, there's no JPA equivalent. You can learn if the object is loaded and than use one of the two options, either accessing the properties while the object is still attached to persistence context, what I typically see is calling size, just for the sake of initializing the collection

myEntity.getListOfThings().size();

or use a query for initialization.