gpt4 book ai didi

jpa - 注释一个字段和它的getter方法JPA的区别

转载 作者:行者123 更新时间:2023-12-03 20:35:31 24 4
gpt4 key购买 nike

我试图弄清楚直接注释(让我们以@id 为例)getter 方法和相关字段之间是否存在差异,如果我注释该字段,JPA 是否使用某种反射来调用相应的 getter?
因为在我的情况下,我试图混淆我的实体类,所以我正在寻找一种方法来保留业务逻辑,因为 getter 将被重命名为类似 aaa() 的东西。

最佳答案

这是 JPA2 规范的第 2.3.1 节所说的:

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.



因此,如果您想避免混淆问题,请一致地注释字段而不是 getter,或者使用 @Access 注释来强制字段访问类型。

关于jpa - 注释一个字段和它的getter方法JPA的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10188670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com