簡體   English   中英

在哪里放置hibernate注釋?

[英]Where to put hibernate annotations?

我把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訪問狀態。 這在實踐中運作良好,是推薦的方法。

注意

在類層次結構中放置注釋必須是一致的(字段或屬性)才能確定默認訪問類型。 建議在整個應用程序中堅持使用單一注釋放置策略。

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

  • 強制實體層次結構的訪問類型
  • 覆蓋類層次結構中特定實體的訪問類型
  • 覆蓋可嵌入類型的訪問類型

最佳用例是可能不使用相同訪問類型的多個實體使用的可嵌入類。 在這種情況下,最好強制使用可嵌入類級別的訪問類型。

(......)

關於兩種風格的利弊,我建議閱讀以下問題:

這取決於你的風格。 你可以把它放在野外或吸氣前。 在嚴格的JPA中,setter上的注釋被忽略,但我不確定Hibernate是否遵循這一點。

您需要在整個實體中保持一致,或者需要在類的頂部使用默認模式提供@Access注釋,並在每個字段/屬性之前提供另一個@Access,以便偏離當前的類模式。

以下是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.

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

眾所周知,Hibernate使用Java反射。 所以你把它放在領域之上還是在吸氣劑上方並不重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM