the difference between anotating a field and its getter method JPA

前端 未结 2 818
予麋鹿
予麋鹿 2021-01-06 21:09

i\'m trying to figure wether there\'s a difference betweeen anotating (let\'s take @id as an example) a getter method and the concerned field directly , in case i annotate

相关标签:
2条回答
  • 2021-01-06 21:31

    JPA allows for two types of access to the data of a persistent class. Field access which means that it maps the instance variables (fields) to columns in the database and Property access which means that is uses the getters to determine the property names that will be mapped to the db. What access type it will be used is decided by where you put the @Id annotation (on the id field or the getId() method).

    0 讨论(0)
  • 2021-01-06 21:42

    Here's what the section 2.3.1 of the JPA2 specification says:

    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. An access type is explicitly specified by means of the Access annotation[6], as described in section 2.3.2. When annotations are used to define a default access type, 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.

    • 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[7], and the persistence provider runtime accesses persistent state via the property accessor methods. All properties not annotated with the Transient annotation are persistent.
    • Mapping annotations must not be applied to fields or properties that are transient or Transient.

    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. Any embeddable classes used by such classes will have the same access type as the default access type of the hierarchy unless the Access annotation is specified as defined below. It is an error if a default access type cannot be determined and an access type is not explicitly specified by means of annotations or the XML descriptor. The behavior of applications that mix the placement of annotations on fields and properties within an entity hierarchy without explicitly specifying the Access annotation is undefined.

    So, if you want to avoid problems with the obfuscation, then annotate the fields and not the getters, consistently, or use the @Access annotation to force field access type.

    0 讨论(0)
提交回复
热议问题