1

As entity class field can be mark as private when @Entity access mode is "Field" access , just wanted to understand how provider will able to access entity state in this case as fields are marked as Private and will not be visible outside class ?

Edit #1 - As you know , for field access , getter and setter method are optional. So I wanted to understand how provider will access the field when no getter/setter method are provided. Hope this clarify my question .

MWiesner
  • 8,868
  • 11
  • 36
  • 70
RaJ
  • 151
  • 2
  • 4
  • 11

2 Answers2

8

Referring to the official JPA specification (final version, JPA 2.1) in Section 2.2 (page 24) we find:

The persistent state of an entity is accessed by the persistence provider runtime either via JavaBeans style property accessors (“property access”) or via instance variables (“field access”). Whether persistent properties or persistent fields or a combination of the two is used for the provider’s access to a given class or entity hierarchy is determined as described in Section 2.3, “Access Type”.

In Section 2.3.1 (page 27) this definition is made more concrete - with respect to your question:

By default, a single access type (field or property access) applies to an entity hierarchy. The default access type of an entity hierarchy is determined by the placement of mapping annotations on the attributes of the entity classes and mapped superclasses of the entity hierarchy that do not explicitly specify an access type. [...]

• When field-based access is used, the object/relational mapping annotations for the entity class annotate the instance variables, and the persistence provider runtime accesses instance variables directly. All non-transient instance variables that are not annotated with the Transient annotation are persistent.

• When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors, and the persistence provider runtime accesses persistent state via the property accessor methods. All properties not annotated with the Transient annotation are persistent.

The term directly refers to an access strategy which allows the manipulation of an object's field (value) without the need to use getter/setter methods. In Java and for most OR-mappers (at least the ones I know of) this is achieved via Introspection - using the Java Reflection API. This way, classes' fields can be inspected for and manipulated to hold/represent data values from the (relational) database entries (i.e., their respective columns).

For instance, the provider Hibernate gives the following explanation in their User Guide:

2.5.9. Access strategies

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy.

Important note:

Be careful when experimenting with different access strategies! The following requirement must hold (JPA specification, p. 28):

All such classes in the entity hierarchy whose access type is defaulted in this way must be consistent in their placement of annotations on either fields or properties, such that a single, consistent default access type applies within the hierarchy.

Hope it helps.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
2

The provider can use reflection to access a private field on a class instance.

KyleM
  • 4,445
  • 9
  • 46
  • 78