-->
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: How do I handle final properties with Hibernate?
PostPosted: Tue Mar 01, 2005 8:28 pm 
Newbie

Joined: Thu Jan 27, 2005 7:50 pm
Posts: 2
Hello,

I am using Hibernate 2, i have searched for an answer and I own the book.

I have a class that has a string property that I do not want to have modified after the first time it is set, so I have my property declared like so:

private final String name;

Hibernate recommends getters and setters for all mapped properties but a final field shouldn't have a setter and will cause a compiler error. I have seen the section in the Hibernate docs advising against the use of final classes, but I can't find anything regarding final properties.

The only thing I can think of is to try configuring Hibernate to access class properties directly. Are there any other options besides configuring Hibernate to directly access instance variables?

I feel like I am missing something. Any help wil be appreciated.

Thanks,
Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well, hibernate uses the default constructor and then "Accessors" to inject the persistent state values onto you POJO. Once an object instance is constructed, you cannot change the value of its nstance variables marked as final. Consider the scenario of reading from the database. How is Hibernate supposed to set the value of that field?

You can ensure property values are not written to the database after insertion using <property ... update="false"/>, but you cannot use final instance vars at the object level.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 3:28 am 
Newbie

Joined: Tue Feb 22, 2005 11:51 am
Posts: 18
and why do you want to make a final propertie persistent? the propertie never changes the value, so you can also hard-code it


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Wed Mar 02, 2005 3:05 pm 
Newbie

Joined: Thu Jan 27, 2005 7:50 pm
Posts: 2
Thanks, Steve. 'update = false' seems like the workaround I am looking for.

Seb to answer your question, this relates to a larger architectural issue. We have two applications that will be used to automate different parts of a larger organizational workflow. Each application currently has it's own database and is not located on the same physical machine. However, we want to be able to synchronize certain data across applications (for instance lists of states, provences, and countries for use in UI choices).

The current scheme is to store these lists of UI choices in the database and then synchronize the tables to the other database. So, in the application I am developing I was hoping that I would be able to use objects with final properties to represent these list elements so that the properties could not be modified after they were set to a value loaded from the db at object initialization time and thereby would not get modified or updated in the database.

It is currently unacceptable to us to hard code the values into the application using an enum, or constants class type solution. So, making the class properties final seemed like the natural solution to me. If anyone could provide alternate suggestions for handling this problem or references where I could find examples, it would be appreciated.

Thanks,
-Jesse


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.