往哪里放休眠注解? [英] Where to put hibernate annotations?

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

问题描述

我在哪里把我的Hibernate注解?

Where do i put my hibernate annotations?

这是我的实例变量上面的行?或吸气过吗?或setter方法​​之前?或者没有真的重要吗?

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

非常感谢

推荐答案

您将它们无论是在的字段的或在的的getter 的。从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标注3.5和JPA 2.0,它的一部分Hibernate的注解扩展):

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注解。
  如果这些注解是上一个字段,
  那么只有场被认为是
  持久性和国家访问
  通过现场。如果有注解
  上的吸气剂,则只有吸气剂
  被认为是持久性和
  状态是通过访问
  的getter / setter。行之有效的
  实践是推荐
  的方法。

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:

  • Hibernate/JPA - annotating bean methods vs fields
  • Hibernate Annotations - Which is better, field or property access?
  • Performance difference between annotating fields or getter methods in Hibernate / JPA

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

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