Here’s my query before I converted over to Spring Data JPA. Notice how I used to use Hibernate.initialize() to manually fetch the widget’s messages.
public Object findWidget(final Widget findMe) {
Widget widget = getJpaTemplate().execute(new JpaCallback<Widget>() {
public Widget doInJpa(EntityManager em) throws PersistenceException {
Query q = em.createQuery("SELECT h FROM " + entityClass.getName() + " h where h.widgetId = ? ");
q.setParameter(1, findMe.getId());
Widget found = (Widget)q.getSingleResult();
//Initialize lazy associations
if(found!= null){
Hibernate.initialize(widget.getMessages());
}
return found;
}
});
return widget;
}
And here’s what my query function looks like now. Notice there is no body to put the Hibernate.initialize() in.
@Query("SELECT h FROM Widget h where h.widgetId = ?1 ")
public AccessPoint findWidget(String widgetId);
So how can I specify that the widget’s messages are to be fetched actively and not lazily?
Try a fetch join, something like this:
http://docs.oracle.com/html/E24396_01/ejb3_langref.html#ejb3_langref_fetch_joins