-->
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: setters/getters must do simple field-access
PostPosted: Mon Jan 10, 2005 11:24 am 
Newbie

Joined: Thu Nov 04, 2004 10:24 am
Posts: 17
Could someone point me to documentation with design guidelines for my setters/getters?

I have legacy code that does some complex logic in the setters (and sometimes getters). This can confuse Hibernate, which call setters/getters freely, seemingly on the assumption that nothing but state underlies these methods.

Example (code below) The setter for the employee's hourly pay does not simply set a field-- it does some minimum-wage logic, which depends on the employee's age, and sets appropriately. This is quite OK for calling from a GUI, but when Hibernate calls it, then (1) we can assume that the value loaded from the DB needs no adjustment and (2) more importantly, the _age field, which is essential to the calculation, may not yet have been set by Hibernate.

Part of the issue here is the mandatory initialization order --and note that the non-default constructor may do initialization not done in the default constructor-- but I have the sense that there is a more fundamental issue here: That certain types of code should never be executed in the setters/getters.

Any general design guidelines?

Joshua

Code:
public class Employee{

   public Employee(int age, int hourlyPay){
      setAge(age);
      setHourlyPay(hourlyPay);
   }
   public Employee(){
   }
   
   private int _age;
   private double _hourlyPay;
   
   public int getAge(){
      return _age;
   }
   public void setAge(int age){
      _age=age;
   }
   
   public double getHourlyPay(){
      return _hourlyPay;
   }
   public void setHourlyPay(double hourlyPay){
      double minimumPay = _age > 17 ? 5.0 : 4.0;//could also write getAge() instead of _age
      _hourlyPay = Math.max(minimumPay,_hourlyPay);
   }
   
}



Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 10, 2005 11:36 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
access="field"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 10, 2005 11:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Won't exactly work if your fields start with an _ but a custom PropertyAccessor can easily do the job.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 5:25 am 
Newbie

Joined: Wed Mar 28, 2007 4:30 am
Posts: 1
christian wrote:
access="field"


I have a similar design and I came to the same conclusion.
However, the hibernate manual says that access="field" is not best practice.

The only other alternative I can think of is:
public setXXX for my normal code
private setXXXFieldOnly for hibernate
Do you think this is a good idea?

Do you think defaulting everything to access="field" is best practice for ddd?


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.