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.  [ 5 posts ] 
Author Message
 Post subject: Default Constructor for Classes
PostPosted: Tue Sep 11, 2007 11:54 am 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
Hi,

I know that NHibernate requires a default no parameter constructor to create the session factory and make an object of the class. But what about objects with dependencies. Like Order must have a Customer.

So, I cannot perform the Constructor Dependency:

public Order(Customer customer)
{
_customer = customer;
}

However, I can perform Setter Dependency. Any thoughts on how to perform Constructor dependency for some entity classes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 1:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
You can have as many overloaded constructors as you wish -- NHibernate does not use them. When loading an entity from database, NHibernate instantiate an instance using the default constructor and set the properties according to the mapping file.

If it is more convenient for you to have the constructor overloaded, by all means do so.

_________________
Karl Chu


Top
 Profile  
 
 Post subject: RE
PostPosted: Tue Sep 11, 2007 1:17 pm 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
Hi,

Thanks for the reply. But what if my entity class does not expose a parameterless constructor. NHibernate will fail because it looks for the parameterless constructor to create an object through reflection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 1:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
It is one of the few requirements imposed by NHibernate -- your entity class must have a default constructor; it can be private though. It makes some design purist cringe. However, the alternative is to write one's own data access code. Having a private default constructor is not so bad in my opinion.

_________________
Karl Chu


Top
 Profile  
 
 Post subject: RE:
PostPosted: Tue Sep 11, 2007 2:34 pm 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
Thanks!

I did not know about the Private Constructor. I thought NHibernate require a public default constructor. Yeah! private constructor sounds good!


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