加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

anotating一个字段和它的getter方法JPA之间的区别

发布时间:2020-12-15 05:08:17 所属栏目:Java 来源:网络整理
导读:我试图弄清楚,在aotating(让我们以@id为例)getter方法和直接关注字段之间存在差异,如果我注释该字段,JPA是否使用某种反射来调用相应的getter? 因为在我的情况下我试图模糊我的实体类,所以我正在寻找一种方法来保持业务逻辑,因为getter将被重命名为类似aaa()
我试图弄清楚,在aotating(让我们以@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批注强制字段访问类型.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读