在哪里放置hibernate注释?

我把hibernate注释放在哪里?

它是我实例变量上面的行吗? 或者在吸气器之前? 或者在二传手之前? 或者它真的不重要吗?

非常感谢

您可以将它们放在场上吸气剂上 。 从Hibernate Annotations参考指南:

2.2.1。 将POJO标记为持久实体

(……)

根据您是否注释字段或方法,Hibernate使用的访问类型将是字段或属性。 EJB3规范要求您对要访问的元素类型声明注释,即如果使用属性访问则使用getter方法,如果使用字段访问,则为字段。 应避免在两个字段和方法中混合注释。 Hibernate将从@Id或@EmbeddedId的位置猜测访问类型。

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

2.2.2.2。 访问类型

默认情况下,类层次结构的访问类型由@Id或@EmbeddedId注释的位置定义。 如果这些注释位于字段上,则仅考虑字段用于持久性,并且通过字段访问状态。 如果注释在getter上,那么只有getter被认为是持久性的,并且状态是通过getter / setter访问的。 这在实践中运作良好,是推荐的方法。

注意

在类层次结构中放置注释必须是一致的(字段或属性)才能确定默认访问类型。 建议在整个应用程序中坚持使用一个单一的注释放置策略。

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

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

最佳用例是可能不使用相同访问类型的多个实体使用的可嵌入类。 在这种情况下,最好在可嵌入的类级别强制访问类型。

(……)

关于两种风格的利弊,我建议阅读以下问题:

  • Hibernate / JPA – 注释bean方法与字段
  • Hibernate Annotations – 哪个更好,是字段还是属性访问?
  • Hibernate / JPA中注释字段或getter方法之间的性能差异

这取决于你的风格。 你可以把它放在野外或吸气前。 在严格的JPA中,对setter的注释会被忽略,但我不确定Hibernate是否遵循这一点。

您需要在整个实体中保持一致,或者需要在类顶部使用默认模式提供@Access注释,并在每个字段/属性之前提供另一个@Access,以便偏离当前类模式。

众所周知,Hibernate使用Javareflection。 所以无论你把它放在领域之上还是在吸气剂上方都没关系。

以下是Hibernate中使用的一些重要注释的描述。

 @Entity: declares the class as an entity (ie 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. 

基于注释的Hibernate中的inheritance映射:hibernate中有三种osinheritance映射。 他们是

1.每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.每个子类层次结构:

 @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类层次结构:

 @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. 

希望有助于了解hibenate中使用的基本注释。