当未定义getter/setter方法时,JPA提供者如何访问(私有)字段值? [英] How does a JPA provider access (private) field values when no getter/setter method are defined?

查看:195
本文介绍了当未定义getter/setter方法时,JPA提供者如何访问(私有)字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于当@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映射器(至少我知道的映射器)中,这是通过

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).

例如,提供商 Hibernate 在其

2.5.9.访问策略

2.5.9. Access strategies

作为JPA提供者,Hibernate可以内省两个实体属性 (实例字段)或访问器(实例属性).默认, @Id注释的位置提供了默认的访问策略.

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.

重要说明:

尝试不同的访问策略时要小心!必须满足以下要求(JPA规范,第28页):

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.

希望有帮助.

这篇关于当未定义getter/setter方法时,JPA提供者如何访问(私有)字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆