JPA implementation patterns: Field access vs. property access

Vincent Partington

Last week my colleague Albert Sikkema blogged about using UUIDs as primary keys. Interesting stuff, thanks again, Albert! This week I will continue the JPA implementation patterns series by discussing the relative merits of field access vs. property access.

The JPA specification allows two ways for the persistence provider to access the persistent state of an entity. The persistence provider can either invoke JavaBeans style property accessors (getters and setters) or access the instance fields of the entity directly. Which method is used depends on whether you have annotated the properties or the fields of an entity.

The JPA 1.0 specification does not allow you to mix access types within an entity or even an entity hierarchy. If you have annotated both fields and properties, the behaviour is undefined. The JPA 2.0 specification has the @Access annotation that makes it possible mix access types within an entity or entity hierarchy.

But the interesting question remains; which access type to use? A question that has been discussed before, but one I couldn't resist commenting on too. ;-)

  • Encapsulation - Property access is said to provide better encapsulation, because directly accessing fields is bad, right? Well actually, using property access obliges you to write getters and setters for all your persistent properties. These methods not only allow the JPA provider to set the fields, they also allow any other user of your entity class to do this! Using field access allows you to write only the getters and setters you want to (they're evil, remember?) and also write them as you want them, for example by doing validation in your setters or some calculation in your getters. In contrast, making these methods smarter when using property access is just asking for trouble.
  • Performance - Some people prefer field access because it supposedly offers better performance than property access. But that is a very bad reason to choose field access. Modern optimizing JVMs will make property access perform just as fast as field access and in any case database calls are orders of magnitude slower than either field access or method invocations.
  • Lazy loading in Hibernate - Hibernate's lazy loading implementation always initializes a lazy proxy when any method on that proxy is invoked. The only exception to this is the method annotated with the @Id annotation when you use property access. But when you use field access there is no such method and Hibernate initializes the proxy even when invoking the method that returns the identity of the entity. While some propose to use property access until this bug is fixed, I am not in favour of basing design decisions on framework bugs. If this bug really hurts your performance you might want to try and get the id of entity with the following code:
    Serializable id = ((HibernateProxy) entity).getHibernateLazyInitializer().getIdentifier()
    

    It's nasty, but at least this code will be localized to where you really need it.

  • Field access in Hibernate - It is good to know that while field access is OK for Hibernate to populate your entities, your code should still access those values through methods. Otherwise you will fall into the first of the Hibernate proxy pitfalls mentioned by my colleague Maarten Winkels.

To summarize I think field access is the way to go because it offers better encapsulation (without it properly managing bidirectional associations is impossible) and the performance impact is negligible (#1 on the performance problems top 10 is still the interplay between the database and your Java app). The only downside are some snafu's in Hibernate's lazy loading implementation that require you to take extra care when using field access.

What access type do you prefer? Do you see any difference in the way field access and property access are implemented in JPA providers other than Hibernate? Please let me know by leaving a comment below. See you at the next JPA implementation patterns blog in which I will talk about mapping inheritance hierarchies in JPA.

For a list of all the JPA implementation pattern blogs, please refer to the JPA implementation patterns wrap-up.

Comments (7)

  1. Tbee - Reply

    July 21, 2009 at 10:11 pm

    Field access. Property access is the interface that my entity exposes to the application, persistency is done on the fields. Those contain the data.

  2. Rahul - Reply

    May 11, 2010 at 12:33 am

    Thank you so much....
    "Serializable id = ((HibernateProxy) entity).getHibernateLazyInitializer().getIdentifier()"

    was exactly what i was looking for only getting the Id of the referenced Object without loading the object and outside of the session.

    Thanks a ton.

  3. luk@synergetics.be - Reply

    June 16, 2011 at 2:48 pm

    there's a mistake in your link to the UUID article

  4. [...] Auf http://blog.xebia.com/2009/06/13/jpa-implementation-patterns-field-access-vs-property-access/ wird ein Workaround beschrieben mittels dem man auf die Id zugreifen kann ohne den Proxy zu initialisieren. [...]

  5. Glauco Todesco - Reply

    February 17, 2014 at 3:40 pm

    HI,
    I used property mapping to make same tests:
    Entity method setsName

    public void setName(String name) {
    this.name = name;
    System.out.println(" Test ");
    }

    Author a = em.find(Author.class, 1);

    Entity method setsName

    public void setName(String name) {
    this.name = name;
    System.out.println(" Test ");
    }

  6. Glauco Todesco - Reply

    February 17, 2014 at 3:44 pm

    Sorry, wrong enter.

    When I call the find method:

    Author a = em.find(Author.class, 1);

    The JPA call 3 times de setName method at the Entity,

    Why?

  7. […] From: http://blog.xebia.com/2009/06/13/jpa-implementation-patterns-field-access-vs-property-access/ […]

Add a Comment