hibernate註解解析

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   使用   sp   for   

最近學習hibernate註解形式配置POJO類,將註解的解析記下來,以備以後使用。

例1.
@Entity
@Table(name="user") public class Flight implements Serializable {    Long id; @Id
@GeneratedValue(generator="generator")
@GenericGenerator(name="generator", strategy = "native") public Long getId() { return id; }  public void setId(Long id) { this.id = id; } }

Hibernate 可以對類的屬性或者方法進行註解。屬性對應field類別,方法的 getXxx()對應property類別。

@Entity

聲明一個類為實體Bean。

@Table

說明此實體類映射的表名,目錄,schema的名字。

@Id

聲明此表的主鍵。

@GeneratedValue

定義主鍵的增長策略。我這裡一般交給底層資料庫處理,所以調用了名叫generator的增長方式,由下邊的@GenericGenerator實現

@GenericGenerator

 

hibernate內部的主鍵增長方式.

關於@GeneratedValue@GenericGenerator的詳細說明,在我的另一篇轉載的文章裡邊有。
@GeneratedValue 與 @GenericGenerator

 

例2.

@Table(name="tbl_sky",
  uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})} )

@UniqueConstraint

將對應的欄位設定唯一性標識

例3.
1 public class Flight implements Serializable {2    @Version 3    @Column(name="OPTLOCK") 4     public Integer getVersion() {} } 

 

@Version 

註解用於支援樂觀鎖版本控制。一般可以用 數字 或者 timestamp 類型來支援 version.

@Column

用於映射對應的欄位,其中參數詳解如下:

        name = "columnName";                                (1)
        boolean unique() default false;                      (2)
        boolean nullable() default true;                     (3)
        boolean insertable() default true;                  (4)
        boolean updatable() default true;                 (5)
        String columnDefinition() default "";            (6)
        String table() default "";                                (7)
        int length() default 255;                                (8)
        int precision() default 0;                               (9)
        int scale() default 0;                                     (10)
(1)     name 可選,列名(預設值是屬性名稱)
(2)     unique 可選,是否在該列上設定唯一約束(預設值false)
(3)     nullable 可選,是否設定該列的值可以為空白(預設值true)
(4)     insertable 可選,該列是否作為產生的insert語句中的一個列(預設值true)
(5)     updatable 可選,該列是否作為產生的update語句中的一個列(預設值true)
(6)     columnDefinition 可選,為這個特定列覆蓋SQL DDL片段 (這可能導致無法在不同資料庫間移植)
(7)     table 可選,定義對應的表(預設為主表)
(8)     length 可選,列長度(預設值255)
(9)     precision 可選,列十進位精度(decimal precision)(預設值0)
(10)   scale 可選,如果列十進位數值範圍(decimal scale)可用,在此設定(預設值0)

 例4.
public class user{    @Transient        private Integer id;    @Basic(fetch=FetchType.LAZY,optional=true)    private String name;    @Transient    public Integer getId() {}
@Temporal(TemporalType.TIME)
public java.util.Date getDatetime() {};
}

@Transient
     被註解成 @Transient 的 getter 方法或屬性,將不會被持久化,

@Basic
    所有沒有定義註解的屬性,等價於在其上面添加了 @Basic註解可以聲明屬性的擷取策略 ( fetch strategy )

    fetch:抓取策略,延時載入與立即載入,optional:指定在產生資料庫結構時欄位是否允許為 null.

@Temporal

    在核心的 Java API 中並沒有定義時間精度 ( temporal precision )。因此處理時間類型資料時,你還需要定義將其儲存在資料庫中所預期的精度。
    在資料庫中,表示時間類型的資料有 DATE,TIME,和 TIMESTAMP 三種精度 ( 即單純的日期,時間,或者兩者兼備 )。 可使用 @Temporal 註解來調整精度。

 

其他屬性:

@Enumerated
@javax.persistence.Enumerated(EnumType.STRING)
value:EnumType.STRING,EnumType.ORDINAL
枚舉類型成員屬性映射,EnumType.STRING指定屬性對應為字串,EnumType.ORDINAL指定屬性對應為資料序

@Lob
@javax.persistence.Lob
用於標註欄位類型為Clob和Blob類型
Clob(Character Large Ojects)類型是長字串類型,實體的類型可為char[]、Character[]、或者String類型
Blob(Binary Large Objects)類型是位元組類型,實體的類型可為byte[]、Byte[]、或者實現了Serializable介面的類。
通常使用惰性載入的方式, @Basic(fetch=FetchType.LAZY)

@SecondaryTable 
@javax.persistence.SecondaryTable
將一個實體映射到多個資料庫表中
如:
@Entity
@SecondaryTables({ 
@SecondaryTable(name = "Address"), 
    @SecondaryTable(name = "Comments") 
})
public class Forum implements Serializable {
@Column(table = "Address", length = 100) 
private String street; 
@Column(table = "Address", nullable = false) 
private String city; 
@Column(table = "Address") 
private String conutry; 
@Column(table = "Comments") 
private String title; 
@Column(table = "Comments") 
private String Comments; 
@Column(table = "Comments") 
}table屬性的值指定欄位儲存的表名稱沒有用 @Column 註解改變屬性預設的欄位將會存在於 Forum 表

hibernate註解解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.