java 在哪里放置休眠注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3286508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to put hibernate annotations?
提问by Chris
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?
它是我的实例变量上方的那一行吗?还是在吸气剂之前?还是在二传手之前?还是真的没有关系?
Thanks a lot
非常感谢
回答by Pascal Thivent
You place them either on the fieldor on the getter. From the Hibernate Annotations Reference Guide:
您可以将它们放在场上或吸气剂上。来自 Hibernate 注释参考指南:
2.2.1. Marking a POJO as persistent entity
(...)
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.
2.2.1. 将 POJO 标记为持久实体
(...)
根据您是注释字段还是方法,Hibernate 使用的访问类型将是字段或属性。EJB3 规范要求您在将被访问的元素类型上声明注释,即如果使用属性访问则为 getter 方法,如果使用字段访问则为字段。应避免在字段和方法中混合注释。Hibernate 会根据@Id 或@EmbeddedId 的位置猜测访问类型。
You might also want to read about the @Accessannotation 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):
您可能还想了解@Access允许强制/覆盖访问类型的注释(在 Hibernate Annotations 3.5 和 JPA 2.0 之前,它是 Hibernate Annotation Extensions 的一部分):
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.
Note
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:
- force the access type of the entity hierarchy
- override the access type of a specific entity in the class hierarchy
- override the access type of an embeddable type
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.
(...)
2.2.2.2. 访问类型
默认情况下,类层次结构的访问类型由@Id 或@EmbeddedId 注释的位置定义。如果这些注解在一个字段上,那么只考虑字段的持久性,并且通过该字段访问状态。如果在 getter 上有注解,那么只有 getter 被考虑用于持久化,并且通过 getter/setter 访问状态。这在实践中效果很好,是推荐的方法。
笔记
注释在类层次结构中的位置必须一致(字段或属性)才能确定默认访问类型。建议在整个应用程序中坚持一个单一的注释放置策略。
但是,在某些情况下,您需要:
- 强制实体层次结构的访问类型
- 覆盖类层次结构中特定实体的访问类型
- 覆盖可嵌入类型的访问类型
最佳用例是由多个可能不使用相同访问类型的实体使用的可嵌入类。在这种情况下,最好在可嵌入类级别强制访问类型。
(...)
Regarding the pros and cons of both styles, I suggest to read the following questions:
关于两种风格的优缺点,我建议阅读以下问题:
回答by Brian M. Carr
It's up to your style. You may put it before the field or before getter. In strict JPA, the annotations on setters are ignored, but I'm not sure if Hibernate follows that.
这取决于你的风格。您可以将其放在字段之前或 getter 之前。在严格的 JPA 中,setter 上的注释被忽略,但我不确定 Hibernate 是否遵循这一点。
You either need to be consistent throughout your Entity, or you need to provide an @Access annotation at the top of the class with a default mode, and another @Access before each field/property you wish to deviate from the current class mode.
您要么需要在整个实体中保持一致,要么需要在类的顶部提供一个带有默认模式的 @Access 注释,并在您希望偏离当前类模式的每个字段/属性之前提供另一个 @Access。
回答by batti
Hibernate is known to use Java reflection. So it really doesn't matter whether you put it above the filed or above the getter.
众所周知,Hibernate 使用 Java 反射。因此,将它放在归档文件上方还是吸气剂上方实际上并不重要。
回答by Ravi Kant
Here is the description of some important annotation used in Hibernate.
这里是对 Hibernate 中使用的一些重要注解的描述。
@Entity: declares the class as an entity (i.e. a persistent POJO class)
@Table: is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is defined the default values are used: the unqualified class name of the entity.
@Id: declares the identifier property of this entity.
@Generated Value: annotation is used to specify the primary key generation strategy to use. If the strategy is not specified by default AUTO will be used.
@Column: annotation is used to specify the details of the column to which a field or property will be mapped. If the @Column annotation is not specified by default the property name will be used as the column name.
Annotations based Inheritance mapping in Hibernate: There are three kinds os inheritance mappings in hibernate. They are
Hibernate 中基于注解的继承映射:在 Hibernate 中有 3 种操作系统继承映射。他们是
1.Table per Class hierarchy:
1.Table per Class 层次结构:
@Inheritance – Defines the inheritance strategy to be used for an entity class hierarchy. It is specified on the entity class that is the root of the entity class hierarchy.
@DiscriminatorColumn – Is used to define the discriminator column for the SINGLE_TABLE inheritance mapping strategies. The strategy and the discriminator column are only specified in the root of an entity class hierarchy or sub hierarchy in which a different inheritance strategy is applied
If the @DiscriminatorColumn annotation is missing, and a discriminator column is required, the name of the discriminator column defaults to "DTYPE" and the discriminator type to DiscriminatorType.STRING.
@DiscriminatorValue – Is used to specify the value of the discriminator column for entities of the given type. The DiscriminatorValue annotation can only be specified on a concrete entity class. If the DiscriminatorValue annotation is not specified and a discriminator column is used, a provider-specific function will be used to generate a value representing the entity type. If the DiscriminatorType is STRING, the discriminator value default is the entity name.
2.Table per sub Class hierarchy:
2.每个子类层次结构的表:
@InheritanceType – Defines inheritance strategy options. JOINED is a strategy in which fields that are specific to a subclass are mapped to a separate table than the fields that are common to the parent class, and a join is performed to instantiate the subclass.
@PrimaryKeyJoinColumn – This annotation specifies a primary key column that is used as a foreign key to join to another table.
3.Table per Concrete class hierarchy:
3.每个具体类层次结构的表:
@InheritanceType – Defines inheritance strategy options. TABLE_PER_CLASS is a strategy to map table per concrete class.
@AttributeOverrides – This annotation is used to override mappings of multiple properties or fields.
@AttributeOverride – The AttributeOverride annotation is used to override the mapping of a Basic (whether explicit or default) property or field or Id property or field.
Hope it help's to get idea on basic annotation used in hibenate.
希望它有助于了解 hibenate 中使用的基本注释。

