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

标签 jpa annotations obfuscation

我试图弄清楚直接注释(让我们以@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/

相关文章:

android - 导出经过签名、模糊处理的 Android 应用程序时如何在 Eclipse 中设置 ProGuard?

java - 使用 Spring 与 Java EE 6 注入(inject) JPA EntityManager

总结其他注解的Java注解

node.js - ElectronJS 代码保护 2018

java - JAX-RS 混合 @FormParam 和 MultivaluedMap<String, String> formParams

spring - 当用@Component注解一个类时,这是否意味着它是一个Spring Bean和Singleton?

php - 如何在 Codeigniter 中包含 'activation code'?

java - 在 Hibernate 中为 LocalDateTime 创建列类型 Datetime

java - 使用 Spring 3.1.3 时 EclipseLink 2.4.1 抛出的 TransactionRequiredException

java - JPA 仅选择一列中的一个参数并将其放入字符串中