I've had this need before as well. I wrote a very simple util class to take care of this. The class has a set of static integers (powers of 2) that represent all the possible associations on a particular POJO. Then I have a method that takes the POJO whose associations are to be intialized and an integer which represents all the association that need to be initialized. Here's an example:
Code:
public class ModelPrepUtil {
/**
* Represents the violation types component of an enforcement.
*/
public final static int VIOLATIONS_TYPES = 1;
/**
* Represents the relief component of an enforcement.
*/
public final static int RELIEFS = 2;
/**
* Represents the defendants/respondants types component of an enforcement.
*/
public final static int DEFENDANTS_RESPONDANTS = 4;
/**
* Represents the environmental justices component of an enforcement.
*/
public final static int ENVIRONMENTAL_JUSTICES = 8;
public final static void initalizeSelectedComponents(Enforcement p_enforcement, int p_initializedAggregates){
if(p_enforcement == null || p_initializedAggregates <= 0){
return;
}
try{
if ((p_initializedAggregates & VIOLATIONS_TYPES) > 0) {
Hibernate.initialize(p_enforcement.getViolationTypes());
}
if ((p_initializedAggregates & RELIEFS) > 0) {
Hibernate.initialize(p_enforcement.getReliefs());
}
if ((p_initializedAggregates & DEFENDANTS_RESPONDANTS) > 0) {
Hibernate.initialize(p_enforcement.getDefendantRespondants());
}
if ((p_initializedAggregates & ENVIRONMENTAL_JUSTICES) > 0) {
Hibernate.initialize(p_enforcement.getEnvironmentalJustices());
}
}catch(HibernateException e){
//do something
}
}
}
I happened to write one for each of our major data model components as we only have 6 or so of them, but this model could fairly easily be generalized if you had a whole lot POJOs to deal with like this.