This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes EJB and other Java EE Technologies and the fly likes Entity - Field-Based Access Vs Property Based Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Entity - Field-Based Access Vs Property Based Access" Watch "Entity - Field-Based Access Vs Property Based Access" New topic
Author

Entity - Field-Based Access Vs Property Based Access

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390

P-230 of EJB3 In Action Book states "If you want to use field-based access, you can declare all your POJO persisted
data fields public or protected"
P-231
"Even if you used field-based access, we recommend that you make the fields private and expose the fields to be modified
by getter/setter method."

The above 2 statements are contradictory... Can we mark our fields private and use them for field-based access ?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Sandeep,

yes, you can make fields private and still use them as entity properties. Persistence providers like Hibernate are capable of accessing even private fields (i guess with reflection, bytecode manipulation or something like this).

The two statements are not really contradictory because of course you're free to make access modifiers less restrictive if it even works with private members. Though this is usually nothing you want to do.

Perhaps I should have noted that "field based access" in this context most probably does NOT mean regular field access in your code but instead it means access from the point of view of a persistence provider!

Marco
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Macro,
Thanks for a quick reply...
Marco Ehrentreich wrote:
The two statements are not really contradictory because of course you're free to make access modifiers less restrictive if it even works with private members. Though this is usually nothing you want to do.

[Sandeep:] Yes i agree that 2 statements are not contradictory but the first statement doesn;t mention about private fields which gives a false impression that fields-based access can;t be private.

Marco Ehrentreich wrote:
Perhaps I should have noted that "field based access" in this context most probably does NOT mean regular field access in your code but instead it means access from the point of view of a persistence provider!

[Sandeep:] I think field-based access means that i need not provide it;s getter and setters. Means the following code is still valid and work :


Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

You got it exactly Your assumptions and the example are all correct!

Both ways have advantages and disadvantages. The getter/setter approach for example allows you to define bean properties for which there doens't even have to be a real class member, i.e. getters which return "calculated" values. On the other hand the field access approach doesn't force you to publich all class members just for the sake of a persistence provider.

I don't know if this will change with Java EE 6 / JPA 2 but it sometimes was annoying for me that you can't mix both ways to specify persistence properties on a per-property basis. It's either field access or getter access.


Marco
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Marco Ehrentreich wrote:
I don't know if this will change with Java EE 6 / JPA 2 but it sometimes was annoying for me that you can't mix both ways to specify persistence properties on a per-property basis. It's either field access or getter access.



I *think* that has changed in JPA2. Although not exactly on the lines of allowing both field and getter access on the same entity. From what i remember, if you have multiple entities extending each other, then with JPA2, the base entity is allowed to have field access and the child entity getter access and vice versa.

[My Blog] [JavaRanch Journal]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Thanks for this information, Jaikiran

I guess there will be good reasons that you can't mix field and getter access within a class. It could be very confusing and "convention over configuration" surely wouldn't work as nicely as it does now.

Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Entity - Field-Based Access Vs Property Based Access