JPA:为什么将注释应用于getter或field [英] JPA: Why the annotations are applied on getter or field

查看:133
本文介绍了JPA:为什么将注释应用于getter或field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么将jpa批注应用于字段或getter方法.如果我尝试在setter方法上应用注释,则编译器将生成错误.因为编译器会忽略setter方法上的注释.他们背后的原因是什么?

why the jpa annotations are applied on field or on getter methods. if i try to apply the annotations on setter method then compiler generate the error. because compiler ignore the annotation on setter method. what is the reason behind them?

推荐答案

这是它的指定方式.根据 JPA规范:

This is is how it's specified. Per JPA Specification:

  • 使用基于字段的访问时,实体类的对象/关系映射注释将对实例变量进行注释,并且持久性提供程序运行时将直接访问实例变量.所有未使用Transient批注进行批注的非临时实例变量都是持久性的.
  • 使用基于属性的访问时,实体类的对象/关系映射注释会注释getter属性访问器[7],并且持久性提供程序运行时会通过属性访问器方法访问持久状态.所有未使用Transient批注进行批注的属性都是持久性的.
  • 映射注释不能应用于瞬态或瞬态的字段或属性.
  • 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.

您有两个选择.使用字段级别注释或属性(getter方法)注释.没有第三种选择.

You have two options. Either use field level annotation or property (getter method) annotation. There is no third option.

这篇关于JPA:为什么将注释应用于getter或field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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