Hibernate學習筆記_ID建置原則

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   使用   sp   strong   

一,xml產生id

 

a)    generator    

<hibernate-mapping>    <class name="com.bjsxt.hibernate.Student">                            <id name="id">            <generator class="native"></generator>        </id>        <property name="age" />        <property name="sex" />        <property name="good" type="yes_no"></property>    </class>    </hibernate-mapping>

 

b)    常用四個:native identity sequence uuid(跨平台 native uuid)

native

根據底層資料庫的能力選擇identity,sequence 或者hilo中的一個。(資料庫自增)

identity

對DB2,MySQL, MS SQL Server,Sybase和HypersonicSQL的內建識別欄位提供支援。 返回的標識符是long, short 或者int類型的。 (資料庫自增)

sequence

在DB2,PostgreSQL, Oracle, SAPDB, McKoi中使用序列(sequence), 而在Interbase中使用產生器(generator)。返回的標識符是long, short或者 int類型的。(資料庫自增)

 二,  註解方式:@GeneratedValue

 

a)   自訂ID

 

b)   AUTO(直接寫 @GeneratedValue 相當如native)

 

         i.   預設:對 MySQL,使用auto_increment

        ii.   對 Oracle使用hibernate_sequence(名稱固定)

   @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }

 

c)   IDENTITY(@GeneratedValue(strategy=GenerationType.IDENTITY))

d)   SEQUENCE(@GeneratedValue(strategy=GenerationType.SEQUENCE))

        i.   @SequenceGenerator(可自訂在資料庫產生指定的sequence名)

@Entity@Table(name="_Teacher")@SequenceGenerator(name="teacherSEQ",sequenceName="teacherSEQ_DB")    //"teacherSEQ"為@SequenceGenerator的標識名  ,"teacherSEQ_DB"為指定到資料庫產生的Sequence名public class Teacher {    private int id;    private String name;    private String title;    private String money;    private Date birthDay;    private ZhiCheng zhiCheng;        @Id    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="teacherSEQ")    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }
............}

 

        

e)   TABLE (可以忘記)

原理:就是在資料庫中建立一個表,這個表包含兩個欄位,一個欄位表示名稱,另一個欄位表示值。每次在添加資料時,使用第一個欄位的名稱,來取值作為添加資料的ID,然後再給這個值累加一個值再次存入資料庫,以便下次取出使用。

@TableGenerator(        name="Teacher_GEN", //建置原則的名稱        table="GENERATOR_TABLE",//在資料庫產生表的名稱        pkColumnName = "pk_key",//表中第一個欄位的欄位名 類型為varchar,key        valueColumnName = "pk_value",//表中第二個欄位的欄位名 int ,value        pkColumnValue="Teacher", //這個策略中使用該記錄的第一個欄位的值(key值)            initialValue = 1,                //這個策略中使用該記錄的第二個欄位的值(value值)初始化值        allocationSize=1//每次使用資料後累加的數值    )@SequenceGenerator(name="teacherSEQ",sequenceName="teacherSEQ_DB")public class Teacher {    private int id;    private String name;    private String title;    private String money;    private Date birthDay;    private ZhiCheng zhiCheng;        @Id    @GeneratedValue(strategy=GenerationType.TABLE,generator="Teacher_GEN")    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }
..........}

 

Hibernate學習筆記_ID建置原則

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.