Nov 8, 2018

Field Access vs. Property Access in JPA

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 2.0 specification has the @Access annotation that makes it possible mix access types within an entity or entity hierarchy.

If an entity has no explicit access mode information, then JPA assumes a default access mode. This assumption is not made at random. Instead, JPA first tries to figure out the location of the @Id annotation. If the @Id annotation is used on a field, then field access mode is assumed. If the@Id annotation is used on a getter method, then property access mode is assumed.