-->
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.  [ 4 posts ] 
Author Message
 Post subject: private constructor or no private constructor?
PostPosted: Thu Jul 26, 2007 8:50 am 
Newbie

Joined: Mon Jul 16, 2007 6:26 am
Posts: 13
Hi NG!

I've just browsed the Hibernate-FAQ on www.hibernate.org a bit:

In the section "Common Problems" there are two questions:

*************************************************

Q1:
I get org.hibernate.HibernateException: CGLIB Enhancement failed: <classname>?

A1:
If you use lazy loading with proxies (default for all classes in Hibernate 3), Hibernate needs to subclass your class at runtime. It can't do this if you have a private no-argument constructor. As per documentation, at least package visibility is required.

A bit further is another question:

Q2:
Hibernate is nontransparent because it requires me to have a default constructor which breaks the API of my class!

A2:
You may declare the constructor private. (Same goes for any property accessors.)


*************************************************

So, what is the truth? May I or may I not declare my default constructor private?

Berti


Top
 Profile  
 
 Post subject: Default Constructor
PostPosted: Thu Jul 26, 2007 8:58 am 
Newbie

Joined: Tue Dec 06, 2005 10:13 am
Posts: 17
As far as I know there should be a default constructor. What do you care having one anyway?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 9:05 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You can declare a private constructor but this will prevent hibernate creating a proxy for your class which might affect performance. Such a class must be marked as lazy="false" in the mapping file to indicate a proxy should NOT be created for the class - otherwise the error in Q1 may occur.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 3:08 am 
Newbie

Joined: Mon Jul 16, 2007 6:26 am
Posts: 13
thatmikewilliams wrote:
You can declare a private constructor but this will prevent hibernate creating a proxy for your class which might affect performance. Such a class must be marked as lazy="false" in the mapping file to indicate a proxy should NOT be created for the class - otherwise the error in Q1 may occur.


Ah, that explains the seeming opposite answers. Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.