-->
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.  [ 3 posts ] 
Author Message
 Post subject: BuildSessionFactory() instantiates every class constructor
PostPosted: Mon Feb 19, 2007 5:58 am 
Beginner
Beginner

Joined: Thu Oct 26, 2006 4:45 am
Posts: 39
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 1.0.3

Mapping documents: not relevant

Code between sessionFactory.openSession() and session.close():

Exception in a default constructor. Used to make sure the object is created with an alternative constructor (must use a parameter).

Debug level Hibernate log excerpt:

"Could not instantiate testobject<Name of Object that throws the exception>


The problem is that while calling "BuildSessionFactory()" all mapped objects will be test-instantiated and than always the default constructor is used.
In code, I can see that the default constructor throws an exception. This is because the programmer wants the object to only instantiate using the alternative constructor.

Like this:

Code:
Class Animal
{
  private string name;
  public Animal()
  { // default constructor is disallowed
     throw Exception("Always give a name for the animal")
  }

  public Animal(string sName)
  {
      this.name = sName
  }



This makes the BuildSessionFactory to throw the NHibernateException. "Could not instantiate testobjectAnimal"

How can I work around this and keep the Animal-class as it is?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 6:07 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Specify unsaved-value attribute for its id and version property (if any).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 6:52 am 
Beginner
Beginner

Joined: Thu Oct 26, 2006 4:45 am
Posts: 39
Thanks, just tested it: the unsaved-value in the id-field of the mappingfile indeed prevents the class from being test-instantiated at BuildSessionFactory().


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