java - 当没有定义 getter/setter 方法时,JPA 提供者如何访问(私有(private))字段值?

标签 java jpa jpa-2.0

@Entity访问方式为“Field”访问时,实体类字段可以标记为private,只是想了解提供者如何在这种情况下将能够访问实体状态,因为字段被标记为 Private 并且在类外不可见?

编辑 #1 - 如您所知,对于字段访问,getter 和 setter 方法是可选的。所以我想了解当没有提供 getter/setter 方法时,提供者将如何访问该字段。希望这能澄清我的问题。

最佳答案

引用官方JPA specification (最终版本,JPA 2.1)在第 2.2 节(第 24 页)中我们发现:

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 页)中,此定义更加具体 - 关于您的问题:

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.

术语直接 指的是一种访问策略,它允许在不需要使用getter/setter 方法的情况下操作对象的字段(值)。在 Java 和大多数 OR 映射器(至少我所知道的)中,这是通过 Introspection 实现的。 - 使用 Java Reflection API .这样,可以检查和操作类的字段以保存/表示来自(关系)数据库条目(即它们各自的列)的数据值。

例如,提供商 Hibernate 在其 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.

重要提示:

尝试不同的访问策略时要小心!必须满足以下要求(JPA 规范,第 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.

希望对您有所帮助。

关于java - 当没有定义 getter/setter 方法时,JPA 提供者如何访问(私有(private))字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38002366/

相关文章:

java - 如何按索引向数组添加值?

java - JPA TABLE_PER_CLASS继承: abstract entity table created

hibernate - JPA 2.0/Hibernate - 为什么 hibernate 在删除之前发出选择计数 (*) 查询

java - 无法运行从 javafx-basic-archetype 生成的包

java - 从没有 API 的网站检索信息

java - JPA 2 实体中的多个 @CollectionTable 导致不相关的外部联接

java - JPA 如何确定哪个类正在调用方法?

java - JPA : How to avoid hardcoding jpa-jar location?

JPA - 插入连接表的正确方法(带有额外的列)

java - ActiveMQ 主题上的并行消息消费