Adam Bien's Weblog

Monday Nov 06, 2006

Field- vs. Property-Based access in JPA

The new Java Persistence API specification allows Field-, as well as, Property Based access to the persistent state of an entity:

"...The persistent state of an entity is accessed by the persistence provider runtime[1] either via JavaBeans
style property accessors or via instance variables. A single access type (field or property access) applies
to an entity hierarchy. When annotations are used, the placement of the mapping annotations on either
the persistent fields or persistent properties of the entity class specifies the access type as being either
field- or property-based access respectively..." [ejb3 persistence spec]

You can choose the strategy by annotating either directly the (also private) members or the corresponding getters and setters. On the contrary to J2EE, entites are now objectoriented and polymorphic so known approaches like state encapsulation etc. are valid again. Because of evil getters and setters, direct field annotation should be preferred. This approach has some advantages:

  1. Only interesting fields are exposed to the outside world.
  2. The state is well encapsulated.
  3. You can declare getters and setters in interfaces, abstract classes or mapped superclasses, and override them in the concrete subclasses. It is easier to define transient contract.

There is only one drawback: the container does not use the getters and setters for value injection. The injection-process cannot be debugged with direct field injection....


[my tweets]  Rss My book: Real World Java EE - Rethinking Best Practices

Kommentare:

I completely agree that Field access is preferable. I'll add a #4 to your list of reasons too :) If your domain objects are not anemic, chances are you'll have lots of methods in them, several of which will start with the words "get" or "set". If you're using Property access you have to annotate each such method as @Transient. That gets old very quickly, and I keep getting tripped up by it because I'm not thinking about persistence when I'm writing domain logic!

Gesendet von Tim Fennell am November 06, 2006 at 02:58 PM CET #

Additionally to the issue with debugging there's another disadvantage of field based access: you can't use AOP to execute some action after a certain property has been set by the persistence provider.

Gesendet von Martin Grotzke am November 07, 2006 at 09:47 AM CET #

To: Martin Grotzke
If you use AspectJ you can define pointcuts for fields. For example:

get ( int Point.x )
->when int Point.x is read
set ( !private * Point.* )
->when any non-private field of Point is assigned

So, why can't you use AOP?

Gesendet von Michael Stachel am November 07, 2006 at 02:47 PM CET #

Senden Sie einen Kommentar:
  • HTML Syntax: Ausgeschaltet
Interviews/About
My Recent Book
Java One 2009
CommunityOne East N.Y.C
JavaONE 2008 Interview
Search
...the last 150 posts
...the last 10 comments
greenfire.dev.java.net
Links
my.netbeans.org
Visitors
License