Hibernate learning note _id generation strategy

Source: Internet
Author: User

One, the XML generation ID

A) generator

<hibernate-mapping>    <classname= "Com.bjsxt.hibernate.Student">                            <IDname= "id">            <Generatorclass= "Native"></Generator>        </ID>        < Propertyname= "Age" />        < Propertyname= "Sex" />        < Propertyname= "good"type= "Yes_no"></ Property>    </class>    </hibernate-mapping>

b) Common four: native identity sequence uuid (cross-platform native UUID)

Native

Choose one of Identity,sequence or Hilo based on the ability of the underlying database. (Database self-increment)

Identity

Provides support for built-in identity fields for Db2,mysql, MS SQL server,sybase, and Hypersonicsql. The returned identifiers are of type long, short, or int. (Database self-increment)

Sequence

In Db2,postgresql, Oracle, sapdb, Mckoi, the sequence (sequence) is used, and the generator (InterBase) is used in generator. The returned identifiers are of type long, short, or int. (Database self-increment)

Second, The Annotated way: @GeneratedValue

A) Custom ID

b) AUTO (direct write @GeneratedValue quite as native)

I. Default: For MySQL, use auto_increment

II. Using Hibernate_sequence for Oracle (name fixed)

@Id    @GeneratedValue    publicint  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 (customizable sequence name generated in database)

@Entity @table (name= "_teacher") @SequenceGenerator (name= "Teacherseq", sequencename= "teacherseq_db")    //"Teacherseq" is the identity name of the @sequencegenerator, "teacherseq_db" is the sequence name specified for the database generation Public classTeacher {Private intID; PrivateString name; PrivateString title; PrivateString money; PrivateDate BirthDay; PrivateZhicheng Zhicheng; @Id @GeneratedValue (Strategy=generationtype.sequence,generator= "Teacherseq")     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }
............}

e) TABLE (can forget)

Principle: is to create a table in the database, this table contains two fields, one field represents the name, and the other field represents the value. Each time you add data, use the name of the first field to take the value as the ID of the added data, and then add a value to the value to the database again for the next fetch.

@TableGenerator (Name= "Teacher_gen",//name of the build policyTable= "Generator_table",//The name of the database-generated tablePkcolumnname = "Pk_key",//field Name Type Varchar,key for the first field in the tableValuecolumnname = "Pk_value",//field name of the second field in the table int, valuePkcolumnvalue= "Teacher",//The value of the first field of the record used in this policy (key value)InitialValue = 1,//The value of the second field of the record (value value) is used in this policy to initialize the valueAllocationsize=1//values accumulated after each use of the data) @SequenceGenerator (name= "Teacherseq", sequencename= "teacherseq_db") Public classTeacher {Private intID; PrivateString name; PrivateString title; PrivateString money; PrivateDate BirthDay; PrivateZhicheng Zhicheng; @Id @GeneratedValue (Strategy=generationtype.table,generator= "Teacher_gen")     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }
..........}

Hibernate learning note _id generation strategy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.