在哪里放置休眠注释? [英] Where to put hibernate annotations?

查看:33
本文介绍了在哪里放置休眠注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里放置我的休眠注释?

Where do i put my hibernate annotations?

它是我的实例变量上方的那一行吗?还是在吸气剂之前?还是在二传手之前?还是真的没有关系?

Is it the line above my instance variable? Or before the getter? Or before the setter? Or doesn't it really matter?

非常感谢

推荐答案

您可以将它们放在 fieldgetter 上.来自 Hibernate 注释参考指南:

You place them either on the field or on the getter. From the Hibernate Annotations Reference Guide:

(...)

取决于你是否注释字段或方法,访问类型Hibernate 使用的将是字段或财产.EJB3 规范要求您在元素上声明注释将被访问的类型,即如果您使用属性,则 getter 方法访问,该字段如果您使用字段使用权.在两者中混合注释应避免使用字段和方法.Hibernate 会猜测访问类型从@Id 的位置或@EmbeddedId.

Depending on whether you annotate fields or methods, the access type used by Hibernate will be field or property. The EJB3 spec requires that you declare annotations on the element type that will be accessed, i.e. the getter method if you use property access, the field if you use field access. Mixing annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.

您可能还想了解允许强制/覆盖访问类型的 @Access 注释(在 Hibernate Annotations 3.5 和 JPA 2.0 之前,它是 Hibernate Annotation Extensions 的一部分):

You might also want to read about the @Access annotation that allows to force/override the access type (prior to Hibernate Annotations 3.5 and JPA 2.0, it was part of Hibernate Annotation Extensions):

默认一个类的访问类型层级由职位定义@Id 或 @EmbeddedId 注释.如果这些注释在一个字段上,那么只考虑字段持久化和状态被访问通过场.如果有注释在吸气剂上,那么只有吸气剂被认为是持久性和状态是通过访问吸气剂/吸气剂.这在练习,是推荐的方法.

2.2.2.2. Access type

By default the access type of a class hierarchy is defined by the position of the @Id or @EmbeddedId annotations. If these annotations are on a field, then only fields are considered for persistence and the state is accessed via the field. If there annotations are on a getter, then only the getters are considered for persistence and the state is accessed via the getter/setter. That works well in practice and is the recommended approach.

注意

注解在类层次结构中的位置必须一致(无论是在田地还是在财产上)是能够确定默认访问类型.建议坚持一个单一的注释放置策略贯穿你的整个申请.

The placement of annotations within a class hierarchy has to be consistent (either field or on property) to be able to determine the default access type. It is recommended to stick to one single annotation placement strategy throughout your whole application.

但是在某些情况下,您需要到:

However in some situations, you need to:

  • 强制实体层次结构的访问类型
  • 覆盖类层次结构中特定实体的访问类型
  • 覆盖可嵌入类型的访问类型

最好的用例是可嵌入的几个实体使用的类可能不使用相同的访问类型.在这种情况下最好强制可嵌入类的访问类型水平.

The best use case is an embeddable class used by several entities that might not use the same access type. In this case it is better to force the access type at the embeddable class level.

(...)

关于两种风格的优缺点,我建议阅读以下问题:

Regarding the pros and cons of both styles, I suggest to read the following questions:

这篇关于在哪里放置休眠注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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