I'm working on an app with some special needs, primarily that the persistent objects do not have empty constructors (and never will). The hibernate manual states over and over that all persistent classes must have an empty constructor. IMHO, such a fatal limitation must be treated with disgust and hacked immediately :-)
Initially I solved this by creating my own Interceptor class and an instantiate() method. Very simple to do. You simply add the attribute access="MyInterceptorClass" to all your properties et al. (you do use an auto-generator, of course:-?). Then you just write your instantiate() method to call your constructor with the proper arguments.
I started with default-lazy="false". (I read everything in on startup.) Unfortunately, anytime I tried to read a complex object, hibernate would create a sql monstrosity to instantiate every reachable object from the one object I was reading. Reading one class literally caused a 100+ table join sql string that was 34K long. Obviously that approach failed.
So I went to default-lazy="true". This caused different problems. The CGLIB implementation ignores the custom Interceptor.instantiate() method and attempts its own instantiation on the CGLIBSubclass using an empty constructor: CGLIBLazyInitializer.getProxy() -> proxy = (HibernateProxy) factory.newInstance();
This causes a fatal error, as there is no empty constructor in the CGLIBSubclass. IMHO, the hibernate integration with CGLIB should not be doing this, but c'est la guerre.
So I'm stuck. I can't use lazy and I can't not use lazy.
Time to hack the hibernate source. Override the factory.newInstance() method in CGLIBLazyInitializer.getProxy() and call the Interceptor.instantiate() method as it should. This works. Voila, we've worked around the "empty constructor" problem.
For people who cannot provide empty constructors, this might be sufficient. I can't speak for this in general, because I only use a very limited portion of hibernate's capabilities.
------
I actually want to do away with the CGLIB entirely. Even though I now use lazy reads, I "immediately" do away with the proxies. I've got it working in my app, but my approach is only elegant insofar as a neat "hackaround". I'd prefer to override more of Hibernate and replace the creation of proxies completely. There are some old threads discussing "extension points" for Tuplizers, EntityModes et al. but they appear not to have made it into the release.
http://forum.hibernate.org/viewtopic.ph ... t=tuplizer
http://forum.hibernate.org/viewtopic.ph ... t=tuplizer
(Note: This one uses the undocumented "tuplizer" element in the hibernate-mapping. I've verified this element doesn't blow up the parser, but I have not explored its use and web discussion is less than slim. Has anyone pursued this?)
-----
P.S. Am I the only one frustrated by the downside of the forum rating system? If you have a useful tip you've figured out, there is no incentive to share it since all you're going to do is waste one of your points...like I just did.