当未定义getter/setter方法时,JPA提供者如何访问(私有)字段值? [英] How does a JPA provider access (private) field values when no getter/setter method are defined?
问题描述
由于当@Entity
访问模式为" Field "访问时,实体类字段可以标记为private
,所以只是想了解提供者在这种情况下将如何作为字段访问实体状态是否标记为私人"并且在课堂外不可见?
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 ?
编辑#1-如您所知,对于字段访问,getter和setter方法是可选的.因此,我想了解在没有提供getter/setter方法的情况下提供程序将如何访问该字段.希望这能澄清我的问题.
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 .
推荐答案
Referring to the official JPA specification (final version, JPA 2.1) in Section 2.2 (page 24) we find:
持久性提供程序运行时可以通过JavaBeans访问实体的持久状态 样式属性访问器(属性访问")或通过实例变量(字段访问").是否持久 属性或永久字段或两者的组合用于提供者访问 给定的类或实体层次结构是按第2.3节访问类型"中所述确定的.
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".
在第2.3.1节(第27页)中,该定义更加具体-关于您的问题:
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. [...]
•使用基于字段的访问时,实体类的对象/关系映射注释 注释实例变量,然后p ersistence provider runtime 访问实例变量 直接.所有未用Transient注释的非瞬态实例变量 注释是持久的.
• 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.
•使用基于属性的访问时,实体的对象/关系映射注释
类注释getter属性访问器,并且持久性提供程序运行时通过属性访问器方法访问持久状态.所有未使用Transient
批注进行批注的属性都是持久性的.
• 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.
术语直接是指一种访问策略,该策略无需使用getter/setter方法即可操纵对象的 field (值).在Java和大多数OR映射器(至少我知道的映射器)中,这是通过 Java反射API .这样,可以检查和操纵类的字段,以保存/表示(关系)数据库条目(即,它们各自的列)中的数据值.
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).