-->
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.  [ 8 posts ] 
Author Message
 Post subject: Order in which setters are called
PostPosted: Fri Jan 09, 2004 10:10 pm 
Beginner
Beginner

Joined: Thu Nov 06, 2003 10:04 pm
Posts: 22
When Hibernate retrieves a persistent instance of a class from the database, is there any particular order in which it calls the setter methods of its properties? As I understand, Hibernate will call the default no-args ctor first to instantiate the instance and then calls the setters.

My question arises because of the following situation. I have a class in which there are two properties. In the setter for the second property, I do some validation based on the value of the first property. If the validation fails I throw a runtime exception. I have another ctor (non-default) which actually takes these two properties as arguments and calls the two setters in the right order. But since Hibernate does not call that non-default ctor. So I am getting that exception on retrieval even though while persisting the property values were correct.

I would appreciate any insight.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 11:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
When Hibernate retrieves a persistent instance of a class from the database, is there any particular order in which it calls the setter methods of its properties?


The order they are specified in the mapping document. (Note that this order also determines the column order in the generated SQL.)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2004 12:11 am 
Beginner
Beginner

Joined: Thu Nov 06, 2003 10:04 pm
Posts: 22
If the first property of ClassA is an instance of another class ClassB, and the second property is a value type (in that order in the mapping), then while constructing ClassA, does Hibernate "completely construct" (meaning call the default ctor and all the setters) the first property instance of ClassB, before calling the setter for the second property?

What I observed was ClassB instance was not completely constructed before the setter for the second property was called. I am a little confused as to why I was seeing this behavior.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2004 12:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No. This is due to Hibernate's two-phase load which is an extremely important and sophisticated performance optimization.

(Note that anyway, no ORM implementation would be able to guarantee this - consider the case of circular references.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 10:23 pm 
Beginner
Beginner

Joined: Thu Nov 06, 2003 10:04 pm
Posts: 22
So is it advisable that, in the setters, I do NOT perform argument validation (and throw runtime exceptions for invalid arguments)? I have noticed that I get those runtime exceptions randomly, meaning it appears Hibernate calls those setters in the right (according to my business class) order sometimes and sometimes in a different order. Can you advise if there is another way of getting around this (without actually having to remove that argument validation from the setters)?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 5:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
use access=field for your properties or supply sperate getters only Hibernate will use.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 5:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Sorry, setters I mean.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 5:41 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Or implement the Validatable interface

_________________
Emmanuel


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