-->
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.  [ 2 posts ] 
Author Message
 Post subject: A question about getter/setters of persistent fields
PostPosted: Tue Feb 07, 2006 3:26 pm 
Newbie

Joined: Sun Feb 05, 2006 1:04 pm
Posts: 1
Hi,

Is it valid to write custom code in setter/getters of persistent fields? For example, is the following a valid Hibernate setter?

Code:
public void setName(String name) {
  this.name = Util.manipulate(name);
}


Also can I invoke a Hibernate-backed DAO method inside a setter/getter? Say like this:

Code:
public List<Article> getArticles() {
return articleDao.getArticleByPersonId(getPersonId());
}


Note: The above code snippets are only provided as an example and are not what I really want to do.

Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 6:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes to both. Custom code inside a mutator will cause the updated value to be written back next time there's a flush; custom code of the sort in your accessor example is pointless, as it's exactly what hibernate proxies do. It might be useful if you're not mapping the Article list; common practice is to put that sort of code in a factory rather than a persisted object, though.

If you want a manipulated value provided to the API user, but the unmanipulated value to be persisted, then you should map the unmanipulated version using private accessors and mutators, and expose an unmapped property:
Code:
public String getName()
{
  return Util.manipulate(getInternalName());
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.