注释一个字段和它的getter方法JPA的区别 [英] the difference between anotating a field and its getter method JPA

查看:24
本文介绍了注释一个字段和它的getter方法JPA的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm trying to figure wether there's a difference betweeen anotating (let's take @id as an example) a getter method and the concerned field directly , in case i annotate the field , does JPA use some kind of reflection to invok the corresponding getter ? because in my case i'm trying to obfuscate my entity classes , so i'm looking for a way to keep the business logic since the getters will be renamed into something like aaa() .

推荐答案

JPA2 规范的第 2.3.1 节说明:

Here's what the section 2.3.1 of the JPA2 specification says:

默认情况下,单一访问类型(字段或属性访问)适用于实体层次结构.实体层次结构的默认访问类型是由属性上的映射注释的位置决定实体类和实体层次结构的映射超类没有明确指定访问类型.访问类型是通过访问注释[6]明确指定,如在第 2.3.2 节中描述.当注解用于定义一个默认访问类型,映射注释的位置实体的持久字段或持久属性类将访问类型指定为字段或分别基于属性访问.

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.

  • 当使用基于字段的访问时,实体类的对象/关系映射注释对实例变量进行注释,并且持久性提供程序运行时直接访问实例变量.所有没有标注的非瞬态实例变量瞬态注释是持久的.
  • 当使用基于属性的访问时,实体类的对象/关系映射注解注解了 getter 属性访问器[7],持久化提供者运行时访问持久化状态通过属性访问器方法.所有未注释的属性使用 Transient 注释是持久的.
  • 映射注释不得应用于瞬态或瞬态的字段或属性.

实体层次结构中访问类型为以这种方式默认的位置必须一致字段或属性上的注释,例如单个、在层次结构中应用一致的默认访问类型.任何此类类使用的可嵌入类将具有相同的访问类型作为层次结构的默认访问类型,除非 Access注释指定如下定义.如果默认是错误的无法确定访问类型且未明确访问类型通过注释或 XML 描述符指定.行为混合放置在字段上的注释的应用程序和实体层次结构中的属性,无需明确指定Access 注释未定义.

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 注释来强制字段访问类型.

So, if you want to avoid problems with the obfuscation, then annotate the fields and not the getters, consistently, or use the @Access annotation to force field access type.

这篇关于注释一个字段和它的getter方法JPA的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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