-->
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: Preservering immutable domain objects
PostPosted: Fri Mar 16, 2007 10:23 am 
Newbie

Joined: Mon May 23, 2005 3:30 pm
Posts: 7
I have an immutable domain object, with no setter mettods for primary key values and no default construtor.
How can I define my mapping file with in Hibernate?
There are 2 ways I can do preserve immutable domain object with Hibernate
1. Making the access level to "field" instead of property and define the fields private. Also make the default constructor private
2. Making the setter methods private and def constructor private.

Are these the best ways to handle this?
I am planning to implement using option 1. Is it a good practice to make the access level "field"

Thanks
J


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 10:38 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
On the class mapping definition, you can set the mutable property to false. e.g. <class name="eg.model.Person" table="PERSON" mutable="false">

http://www.hibernate.org/hib_docs/v3/re ... tion-class


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 10:51 am 
Newbie

Joined: Mon May 23, 2005 3:30 pm
Posts: 7
But what will happen when the object is made transient or detached. If I service layer is using this object, and if I have public setter methods, they can still modify the object.

I do not think mutable="false" solves the problem

Thanks
J


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 11:58 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Mutable="false" will just ensure that Hibernate won't persist any changes to the object...never a bad idea, just in case you use bytecode instrumentation or some other framework bypasses the scopes. If you want to make your setters private, Hibernate won't care, it will still be able to set the properties. That way no other process can change the field values.

The manual wrote:
The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you,
using Java Reflection. The constructor can be private, however, package visibility is required for runtime proxy
generation and efficient data retrieval without bytecode instrumentation.

And
The manual wrote:
Properties need not be declared public - Hibernate can persist a property with a default, protected or private
get / set pair.

And
The manual wrote:
You can see that Hibernate can access public, private, and protected accessor methods, as well as
(public, private, protected) fields directly. The choice is up to you and you can match it to fit your application
design.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 1:45 pm 
Newbie

Joined: Mon May 23, 2005 3:30 pm
Posts: 7
Thanks for you reply.

I am not concerned with persisting Domain Object. I am more concerned with this immutable domain object being modified because of these public setters methods.

The only way I can get around the problem is by making access to private scope.

I just wanted to know whether cause any problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 19, 2007 8:57 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Should not cause any problems for Hibernate if you use private setters.


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.