does anyone know how to prevent hibernate from loading a collection or many-to-one assocation?
i have a use case where i need to load an object, but the calling program can determine whether certain properties are loaded or not. the same goes for part of a collections: certain rows have to be fetched, others not.
domain classes:
public class SomeClass {
private SomeClass parent;
private Set<OtherClass> collection;
}
public class OtherClass {
private Date startDate;
private Date endDate;
}
public class Dao {
public SomeClass loadClass(long id, boolean parents, boolean historicalData) {
// load SomeClass
// if parents == true, load parent, otherwise leave <null>
// if historicalData == false, load OtherClass where endDate == <null>
}
}
I have thought of 2 solutions, but I ont to now if its possible with an criteria or query.
Solution 1 is don't make the assocation from SomeClass to OtherClass and the parent/child relation in the hibernate configuration and load the assocation in the code.
Solution 2 is to define different mappings with different entity-names with serve the special cases.
In this case the caller can be in an other JVM or transaction, so the session is closed, thus lazy loading is not an real option.