-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to work around the "empty constructor" problem
PostPosted: Tue Dec 06, 2005 5:14 pm 
Beginner
Beginner

Joined: Sat Oct 29, 2005 2:05 am
Posts: 21
Location: Kansas City, KS
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 5:50 pm 
Beginner
Beginner

Joined: Tue Apr 05, 2005 4:27 pm
Posts: 40
Location: canada
as far as useful tips go, it's intended that they go on the wiki, and not on the forum.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 6:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what constructor do you call instead of the empty constructor ?

(the rest of your posting looks like you are misusing hibernate la 'grande ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 7:28 pm 
Beginner
Beginner

Joined: Sat Oct 29, 2005 2:05 am
Posts: 21
Location: Kansas City, KS
>what constructor do you call instead of the empty constructor ?

public PersistentObj(MySpecialObject1 o1, MySpecialObject2 o2) {}

The details don't matter, besides the fact that public PersistentObj() {} makes no sense in my domain and is not allowed. I couldn't even provide one without causing an Exception.

...Ok, ok, its a distributed system and every object must have a "creator" and "owner", supplied at instantiation time. You literally can't do anything with an object until it has a creator and owner.

(This decision was made from expertise, not poor developer skills, btw, not that that should make any difference.)

>(the rest of your posting looks like you are misusing hibernate la 'grande ;)

Must be a communication issue :-) Bear in mind I've spent the last few weeks overcoming one issue after another in order to make hibernate work with my system. Slow slogging. The near-fatal problem was lack of support for list-to-list, but the rest has simply been me needing to do things in a way that no one else apparently does. I am definitely using hibernate in ways in which the designers never intended.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 7:33 pm 
Beginner
Beginner

Joined: Sat Oct 29, 2005 2:05 am
Posts: 21
Location: Kansas City, KS
>as far as useful tips go, it's intended that they go on the wiki, and not on the forum.

Ah. Ok. Thanks. I've just been finding much more useful information in the forums, so I figured this is where it should go.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 11:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Tuplizers are in 3.1 (I believe since rc1). They are discussed in the 3.1 documentation (granted it is light) and even have some tests in the test suite specifically as an extension point (for example http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/dynamicentity/tuplizer/ ).

While Tuplizers themselves are very mature (they grew out of refactorings to Persisters), Tuplizers as an extension point are very immature, but the intent is exactly the type of things you are trying. Unfortunately noone using the tuplizer extensions is actually creating jira issues for the stuff they find (sorry I don't have time to scour the forums for potential bug reports to add to jira myself). Bottom line is that Tuplizers are definitely the way to achieve what you want, but expect to run into bugs; please just report them so I can fix them ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.