Primary key is a basic concept in the relational database, it is used to ensure that the unique primary key of the record has no business meaning, so the developer does not, and does not need, to set the primary key value of the entity object. But for the database, the primary key is necessary hibernate has two types of policies: one is the JPA standard primary key generation strategy, and the other is the Hibernate framework-specific primary key generation strategy. There are 4 types of JPA standard strategies:
1.auto Policy (JPA Default policy) such as: public class Teacher {private int id; Private String title;
@Id @GeneratedValue (strategy = generationtype.auto) public int getId () {return Id; }} I use the 2.table policy (the primary key value that is required to save the inserted record) such as: public class Teacher {private int id; Private String title;
@Id @GeneratedValue (strategy = generationtype.table,generator= "Mytablegenerator") @ Tablegenerator (name = "Mytablegenerator", table = "Hibernateneedtable", pkcolumnname = "Pk_key", Valuecolumnname = "Pk_value", Pkcolumnvalue = "Teacherid", InitialValue = Allocationsize = +) public int getId () {& nbsp; return ID; } 3.sequence policy (in order to use sequences) such as: public class Teacher { private int id; private String title;
@Id @GeneratedValue (strategy = generationtype.sequence,generator= "Myseqgenerator") @SequenceGenerator (name = " Myseqgenerator ", Sequencename =" T_teacher_sequence ", InitialValue = +, allocationsize =) public int getId () { return ID; }} You need to use @sequencegenerator to specify information about the sequence. Name: the names of the sequence generators, which are referenced in @generatedvalue
Sequence generator name in the Sequencename:oracle database
InitialValue: Initial value of primary key
Allocationsize: The size of each growth value of the primary key note: If the underlying database does not execute the sequence, an error is noted:
Org.hibernate.MappingException:org.hibernate.dialect.MySQLDialect does not support sequences
4.identity Policy (provides support for self-increasing primary keys) such as: public class Teacher {private int id; Private String title;
@Id @GeneratedValue (strategy = generationtype.identity) public int getId () {return Id; } }
Second, summary
There are three types of primary keys for a data model in hibernate: UUID, native, assigned, respectively, universal unique identifier, self-increment, and custom. 1, the UUID is the system generated, insert the database is much faster than native, but the UUID is a long list of unordered strings, in theory, it will be slower to find, but it will not affect the development. The UUID is a universal unique identifier, it is not two choices when you do not know how to define the primary key, 1.1, the generation of simple, Java has already had something ready, introduced Java.util.UUID, 1.2, universal only, in fact, can add the global, UUID generation principle: The current date and time ( The first part of the UUID is related to the time, if you generate a UUID after a few seconds to generate a UUID, then the first part is different, the rest is the same), the clock sequence, the globally unique IEEE Machine identification number (if there is a network card, from the network card, no network card obtained in other ways); 1.3 There is no primary key conflict between two data in the same data table at any time, which can be a significant benefit in real-world development. 2, and native is the database generation, the insert will be calculated first, so it will be slower than the UUID, but it is easier to find and delete data. 3, UUID and assigned generation is done in the program, one is automatic, one is manual. Therefore, when Session.save () is performed, no SQL statements are generated and no data is available in the database. The native needs to read the database data to complete the auto-increment, so when the Session.save () is executed, the corresponding SQL statement is generated, and the data is in the database.
Hibernate primary Key generation