I recently went through a porting excercise where I was converting a huge domain mode (250+ classes) from using ObjectStore to Hibernate3. This presented some interesting challenges as this model had evolved over 5 years and was in its current state very object oriented. I probably touched most mapping aspect/challenges possible and learned alot about hibernate and ORM/caching/TX in general.
One issue that I found kind of hard to get around was the usage of Proxied classes (all classes by default in Hibernate3 - setting lazy="true") and type casting.
Looking aroung the net I found the recommendation..
specify lazy="false" for those classes where you need to type cast. In my case that meant making all baseclasses non-lazy. I tried that and can only say...
bye bye to performance. It just doesnt work. The type of domain model I was working with (not an option to rewrite) I had to find another solution. We are talking a magnitude slower with lazy set to of.
Well I wrote my own narrower that I want to chare with you. Please have a go and see what you think. This utility class retreives that implementation of the proxy and works fine... I am using it at the moment. The performance is great.
Code:
public class ProxyConverter {
public static Object convert(Object object)
{
if (object instanceof HibernateProxy) {
HibernateProxy proxy = (HibernateProxy) object;
LazyInitializer li = proxy.getHibernateLazyInitializer();
return li.getImplementation();
}
else {
return object;
}
}
}