My problem is relatively simple:
I have an entity with many lazily loaded Sets that I don't need all the time. Now I don't want to load everything eagerly, nor do I want to have several different getSlimEntity(), getFullEntity(), getEntityForSpecialOccasion() methods.
What I'd like to do is having the 'slim' version by default and then load the lazy stuff on demand. That demand could originate in changing a tab on a web page displaying a 'task' entity. The new tab should display the task's history. This tab isn't used very often, though, so I don't want the data eagerly.
A solution should put as little work as possible on the database since database performance this is a problem for us.
A possible solution I came up with is using Session.buildLockRequest() to 're-attach' the task to the EntityManager and the call Hibernate.initialize() on the Set I need. It looks like this:
public Set<HistoryEntry> getHistory(Task task) {
Session session = manager.unwrap(Session.class);
session.buildLockRequest(LockOptions.NONE).lock(task);
Hibernate.initialize(task.getHistory());
return task.getHistory();
}
It's a slim and relatively easy-to-understand solution, also the database isn't updated or anything, just queried for the HistoryEntries in question.
Now my question(s): Is this solution okay to use? Does it pose any problems I don't see? What is actually happening by calling buildLockRequest? Also, could there be problems when multiple users view the same task?
// Edit: As pointed out, this solution uses Hibernate specific calls that may not be compatible with other Persistence Providers, such as EclipseLink or OpenJPA. Since this project is internal only, that should not be a problem.