Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Field vs Property Access
PostPosted: Wed Apr 25, 2007 10:55 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
Which is better, Field or Property access, and why?

Or perhaps a better phrasing would be, what types of situation are better handled by using Field access rather than Property access and vica versa?

I'd appreciate some feedback on what access type people are using and why.

As a Java programmer my gut reaction is to go with property access, since it preserves the encapsulation. But in reality, persistence is always going to be tightly tied to the internal structure of my object, and I can't see that adding private getters or setters specifically for hibernate actually provides a lot of benefit.

There are some benefits I can see for using property access
  • I can initialize some transient objects that are dependant on the data from hibernate. This is by far the most convincing use case I can come up with.
  • When unexpected values are appearing, it's easier to debug if I can have a debug point on the setter method
  • If I want to change the internal structure but not the schema, I can do so and do the data transformation in the getter and setter

The last two cases are particularly flimsy, but all these cases I'm thinking would be better handled by using field access in most cases and just using the @AccessType annotation to change the access to property for those few fields that need it.

On the other hand, there are some serious disadvantages to property access. First off
  • More Code. Yes, any decent IDE can generate the getters and setters for me, but they're still a maintenance burden. They also clutter up the class making it harder to see at a glance where the actual logic is.
  • When I want null checks or other such business logic in my setters or getters I almost always need two sets of accessors, a private set for hibernate to use without the logic, and a public set for real code to use. This is particularly annoying.


All of this leads me to think I should be using field access most of the time and just overriding it when I need some additional behaviour. But hibernate defaults to using property access which would indicate to me that the Hibernate team believe it's the best option.

Is there some major advantage of property access I'm missing? Or is it just a matter of preference and coding style?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 3:46 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
I was just about to post the same question - glad I did a search first.

I too would like to know the advantage/disadvantage. I'm having some issues that would be best solved by just setting all access to field instead of property, but am wondering if this is wise if, as mentioned by EdC, the default is property.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 3:50 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Property access allows you to call foo.getId() without initializing a proxy.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 3:55 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
christian wrote:
Property access allows you to call foo.getId() without initializing a proxy.


Is that the only real benefit? I currently haven't implemented a separate set of accessors for (N)Hibernate, and rarely want to access only the ID of an entity. Therefore would I be better off going to the trouble of implementing a separate set of accessors or just changing all of my access to field?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 4:48 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
Access methods let you make use of things like AOP.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 5:30 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I use field access by default and enable property access if I implement certain design patterns (Delegate for example) or need the extra lazy behavior of a proxy identifier.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 6:49 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
Can only the OP rate an answer? I'd hate for any more kittens to die....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 7:05 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
I think the admin staff should impliment a dead kitten counter. >.> But then you'd need some kind of cunning algorythem to assess if a post was helpfull or not. ^_^

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject: Re: Field vs Property Access
PostPosted: Tue Apr 27, 2010 1:49 pm 
Newbie

Joined: Thu Apr 01, 2010 6:09 pm
Posts: 8
I also have a similar question and when I was browsing the Internet, I compiled all related info and posted the same in this article Though late, i thought i would post it here for you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 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.