Hello there! Since Hibernate 3 all associations are lazy This brings a bit of problem with lazy instatiation, I guess.
Let's examine this example
User
car : Car (lazy)
Car
manufacturer: Manufacturer (lazy)
Manufacturer
id
name
Ok, So I load User and then Hibernate.Initialize(car); that's ok. So in my view tier:
user.getCar().getManufacturer().getName() -> exception.
Today on Hibernate 2 I have an utility method that initializes every collection I need on a given class, but it lacks initialization on deeper object associations like the one above, this is my method:
public Entity initialize(Entity entity, String[] initialize){
try{
Session session = getCurrentSession();
session.lock(entity,LockMode.NONE);
if(initialize != null){
for(int i=0;i<initialize.length;i++){
Hibernate.initialize(PropertyUtils.getProperty(entity,initialize[i]));
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
closeSession();
}
return entity;
}
How can one accomplish that?
Regards
|