Java 在哪里放置hibernate注释?

Java 在哪里放置hibernate注释?,java,hibernate,annotations,Java,Hibernate,Annotations,我应该把我的hibernate注释放在哪里 是我的实例变量上面的那一行吗?还是在盖特之前?还是在二传手之前?或者这真的不重要吗 非常感谢这取决于你的风格。你可以把它放在场前或getter之前。在严格的JPA中,setter上的注释被忽略,但我不确定Hibernate是否遵循这一点 您需要在整个实体中保持一致,或者需要在类的顶部提供一个带有默认模式的@Access注释,并在希望偏离当前类模式的每个字段/属性之前提供另一个@Access注释。您可以将它们放在字段或getter上。从《Hibernat

我应该把我的hibernate注释放在哪里

是我的实例变量上面的那一行吗?还是在盖特之前?还是在二传手之前?或者这真的不重要吗


非常感谢

这取决于你的风格。你可以把它放在场前或getter之前。在严格的JPA中,setter上的注释被忽略,但我不确定Hibernate是否遵循这一点


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

您可以将它们放在字段或getter上。从《Hibernate注释参考指南》中:

(……)

取决于是否注释 字段或方法,访问类型 Hibernate将使用字段或 财产。EJB3规范要求 您可以在元素上声明注释 将被访问的类型,即 如果使用属性,则使用getter方法 访问,如果使用字段,则为字段 通道在两者中混合注释 应避免使用字段和方法。 Hibernate将猜测访问类型 从@Id或 @嵌入ID

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

默认情况下,类的访问类型 层次结构由职位定义 @Id或@EmbeddedId注释的。 如果这些注释位于字段上, 然后只考虑字段 持久性和状态被访问 通过现场。如果有注释 在一个吸气剂上,然后只有吸气剂 考虑持久性和 状态是通过 盖特/塞特。这在中国很管用 推荐的做法是 接近

注意

类层次结构中注释的位置必须一致 (字段或属性上)将 能够确定默认访问权限 类型。建议您坚持使用 单个注释放置 战略贯穿你的整个人生 应用程序

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

  • 强制实体层次结构的访问类型
  • 重写类层次结构中特定实体的访问类型
  • 重写可嵌入类型的访问类型
最好的用例是可嵌入的 由多个实体使用的类 可能不使用相同的访问类型。在里面 在这种情况下,最好强制 可嵌入类的访问类型 水平

(……)

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


  • 众所周知,Hibernate使用Java反射。因此,将其放在字段上方或getter上方并不重要。

    下面是对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.
    
    Hibernate中基于注释的继承映射: hibernate中有三种os继承映射。 是的

    1.每个类层次结构的表:

    @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.
    
    @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.
    
    @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.
    
    2.每个子类层次结构的表:

    @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.
    
    @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.
    
    @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.
    
    3.每个混凝土等级层次结构的表:

    @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.
    
    @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.
    
    @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中使用的基本注释。

    那么您是说如果我将注释放在字段中,那么get/setter将不会用于任何用途?对于不需要注释的简单(如字符串)字段,会使用它们的字段或setter吗?@Chris:如果使用字段访问,Hibernate将绕过get/set对,使用反射直接访问字段。关于你评论的第二部分,这完全取决于访问类型——字段或属性——可以从@Id或@EmbeddedId的位置猜出。我发现如果我使用字段访问,Hibernate用于延迟加载的代理会遇到各种各样的问题。所以,这是一个成功者/成功者!呜呜呜代码生成来营救!该死的,java的东西太冗长了@克里斯:我个人也在使用财产(见)。@PascalThivent-嗨!我的书《Hibernate变得容易》告诉我要把它摆在能手和二传手面前。你能告诉我为什么有人会这么说吗?类似地,
    @Id@GeneratedValue
    应该在
    getId()
    之前,它返回类的
    Id
    变量。我想知道为什么。我发现如果我使用字段访问,Hibernate用于延迟加载的代理会遇到各种各样的问题。所以,这是一个成功者/成功者!呜呜呜代码生成来营救!该死的,java的东西太冗长了事实上,我已经与延迟加载问题斗争了一段时间(将我的模型设置为使用即时抓取,以便在它只是一个原型的情况下让它工作!)但是,是的,getter/setter对hibernate非常重要。我的书《hibernate轻松版》告诉我把它放在getter和setter之前。我不知道为什么。