anotating场和getter方法​​JPA之间的差异 [英] the difference between anotating a field and its getter method JPA

查看:200
本文介绍了anotating场和getter方法​​JPA之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出羯羊有betweeen anotating(让我们@id为例)getter方法​​,并直接有关领域有所作为,如果我注释字段,并JPA使用某种反射到invok的相应的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],和持久性提供运行时访问持久
      通过属性访问方法的状态。所有属性没有注解
      与瞬态标注是永久性的。

  •   
  • 映射注释不能应用于那些瞬时或瞬态字段或属性。

  •   

所有这些类在实体层次的访问类型
  在这种方式默认必须在其放置一致
  在任字段或属性的注释,使得单个,
  一致的默认访问类型应用层次中。任何
  通过这样的类使用嵌入类将具有相同的访问类型
  作为层次结构,除非访问的默认接入类型
  如下所定义被指定注释。这是,如果一个默认的错误
  访问类型无法确定和接入类型是没有明确
  通过注解或XML描述符的方式指定。行为
  那场对混合注解的位置应用和
  没有明确指定一个实体层次结构中的属性
  访问注释是不确定的。

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.

所以,如果你想避免混淆的问题,那么注释字段,而不是干将,始终如一,或使用@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.

这篇关于anotating场和getter方法​​JPA之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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