I have hibernate pojo class A { B b ; some other properies} with lazy= true for class B.
When i get object A, B is not loaded and hibernate returns its proxy. When i pass this object to another module, that module traverse each and every objects in A and when it encounter B.getXXX it throws LazyInitialization exception. In this particular case, I do not want to load class B as it is not required. Is there any way when i call methods on B it either return null or turn proxy of B into real object B so that module doesn’t throw LazyInitialization error. I cannot change class B getter,setter as it common class and use by many other classes.
I have hibernate pojo class A { B b ; some other properies} with
Share
Thanks for all your suggestion.
My application have layered architecture. Service->Manager->Dao. Hibernate session closes after manager. Other module interacts only through Service. Opening hibernate session till request complete is not an option for me. I also do not want to hit database as it is not necessary that properties of B are populated. I just want to replace hibernate proxy with real object so that anyone who is using service do not face any problem. I found a utility at
http://svn.rhq-project.org/repos/rhq/branches/HEIKO-EXP/modules/enterprise/server/safe-invoker/src/main/java/org/rhq/enterprise/server/util/HibernateDetachUtility.java
which exactly does what i want. It inspect object and replace hibernate proxy with real object. I need to customize following things in above utility
1. Change instances of classname from org.rhq to my package structure.
2. They expect name of identity field in pojo is “id”. I change it to use those property which has annotation of javax.persistence.Id.
Basic testing with above changes is done and it is working fine. I just need to test whole application with various scenario so that it is working in all scenario.