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