Minor clarification, the client of the entity (ie your application) should not access the fields directly - regardless of the access type.
>From section 2.1 of the JPA 1.0 spec The persistent state of an entity is represented by instance variables, which may correspond to Java- Beans properties. An instance variable may be directly accessed only from within the methods of the entity by the entity instance itself. Instance variables must not be accessed by clients of the entity. The state of the entity is available to clients only through the entity’s accessor methods (getter/setter methods) or other business methods. Instance variables must be private, protected, or package visibility. Regarding advantages and disadvantages of different access types it really depends on the application. The reason (AFAIK) for the difference in behavior is to allow for business logic in the accessor methods. When you have business logic there it may make sense for the JPA provider to use FIELD access (and avoid your logic). If there's no business logic then PROPERTY may be the way to go. hth, -mike On Thu, Jun 11, 2009 at 2:24 PM, Daryl Stultz <[email protected]> wrote: > On Thu, Jun 11, 2009 at 2:20 PM, Simon Droscher < > [email protected]> wrote: > > > > > The major point it makes is this: > > When using property access, only the getter and setter method for a > > property > > should ever access the underlying persistent field directly > > > That's exactly what I was looking for, thanks. > > Note that none of this is relevant if you are using field access rather > than > > property access. > > > > But I didn't expect that! > (from the manual) > > > To use field access for an entity using annotation metadata, simply place > >> your metadata and mapping annotations on your field declarations: > >> > > > I am using field access. I placed my annotations on the fields simply > because I like them there. I didn't realize there was a functional > difference. Is there any advantage/disadvantage to field vs property > access? > It seems property access has the potential gotcha, while field does not. > Perhaps there is some other cost... > > -- > Daryl Stultz > _____________________________________ > 6 Degrees Software and Consulting, Inc. > http://www.6degrees.com > mailto:[email protected] >
