Iv. ID Generation Policy 1: XML configuration ID adds a generator sub-element to the id element, which has the class attribute. Common class attributes include: (1) increment: used to generate a unique identifier for long, short, or int type. Data can be used only when no other process inserts data into the same table. Do not use it in a cluster. (Pole
Iv. ID Generation Policy 1: XML configuration ID adds a generator sub-element to the id element, which has the class attribute. Common class attributes include: (1) increment: used to generate a unique identifier for long, short, or int type. Data can be used only when no other process inserts data into the same table. Do not use it in a cluster. (Pole
Iv. ID generation policy first: XML configuration ID
Pass Add Element Child element, which has the class attribute. Common class attributes include:
(1) increment: used to generate a unique identifier for long, short, or int type. Data can be used only when no other process inserts data into the same table. Do not use it in a cluster. (Rarely used)
(2) native: enables the database to automatically select identity, sequence, or others.
(3) uuid: 128-bit UUID algorithm, which generates a String type ID
(4) identity: provides support for the built-in identity fields of DB2, MySQL, SQL Server, Sybase, and HypersonicSQL. The returned identifier is of the long, short, or int type.
Sequence: use sequence in objective El, PostgreSQL, SAP, DB, Mckio, and generator in Interbase. The returned identifiers are of the long, short, or int type.
Lab 1:
(1) create Student. java
package com.zgy.hibernate.model;public class Student {private String id;private String name;private int age;private int score;public int getScore() {return score;}public void setScore(int score) {this.score = score;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
(2) In Student. hbm. xml Set the generator class to uuid.
(3) test
Package com. zgy. hibernate. model; import static org. junit. assert. *; import java. util. date; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. annotationConfiguration; import org. junit. afterClass; import org. junit. beforeClass; import org. junit. test; public class HibernateIDTest {public static SessionFactory sf = null; @ BeforeClasspublic static void beforeClass () {sf = new AnnotationConfiguration (). configure (). buildSessionFactory () ;}@ Testpublic void testStudent () {Student s = new Student (); s. setName ("Zhang San"); s. setAge (20); s. setScore (90); Session session = sf. openSession (); session. beginTransaction (); session. save (s); session. getTransaction (). commit (); session. close () ;}@ AfterClasspublic static void afterClass () {sf. close ();}}
(4) Verification
Select * from student; + dimensions + -------- + ------ + | id | name | age | score | + dimensions + ------ + ------- + ------ + | dimensions | Zhang San | 20 | 90 | + ---------------------------------- + ------ + ----- + ------- + ------ + desc student;
Data is successfully inserted. The id type is varchar (255) and the primary key is used.
Lab 2:
(1) Modify Student. java. Change id to int type
package com.zgy.hibernate.model;public class Student {private int id;private String name;private int age;private int score;public int getScore() {return score;}public void setScore(int score) {this.score = score;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
(2) In Student. hbm. xml Set the generator class to native.
(3) test (delete the student table)
(4) Verification
Select * from student; + ---- + ------ + ------- + | id | name | age | score | + ---- + ------ + ------- + | 1 | Zhang San | 20 | 90 | + ---- + ------ + ----- + -------- + desc student;
Data inserted successfully. id: int (11) type, primary key, Auto_increment
Type 2: Annotation configuration ID
@ GeneratedValue: the Default policy is auto. auto is equivalent to native configured in XML.
Lab 1:
(1) Compile Teacher. java. Add @ GeneratedValue to the getId () method
package com.zgy.hibernate.model;import java.util.Date;import javax.annotation.Generated;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.EnumType;import javax.persistence.Enumerated;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;@Entity@Table(name="_teacher")public class Teacher {private int id;private String name;private String title;private String address;private String wifeName;private Date birth;private ZhiCheng zhiCheng;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}@Column(name="_name")public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getWifeName() {return wifeName;}public void setWifeName(String wifeName) {this.wifeName = wifeName;}@Temporal(TemporalType.DATE)public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}@Enumerated(EnumType.STRING)public ZhiCheng getZhiCheng() {return zhiCheng;}public void setZhiCheng(ZhiCheng zhiCheng) {this.zhiCheng = zhiCheng;}}
(2) Compile TeacherTesting. java for testing.
Package com. zgy. hibernate. model; import static org. junit. assert. *; import java. util. date; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. annotationConfiguration; import org. hibernate. cfg. configuration; impo [this article from the Internet (http://www.68idc.cn)] rt org. junit. afterClass; import org. junit. beforeClass; import org. junit. test; public class TeacherTesting {public static SessionFactory sf = null; @ BeforeClasspublic static void beforeClass () {sf = new AnnotationConfiguration (). configure (). buildSessionFactory () ;}@ Testpublic void test () {Teacher t = new Teacher (); t. setName ("t1"); t. setTitle ("advanced"); t. setAddress ("Beijing"); t. setBirth (new Date (); t. setZhiCheng (ZhiCheng. a); Session session = sf. openSession (); session. beginTransaction (); session. save (t); session. getTransaction (). commit (); session. close () ;}@ AfterClasspublic static void afterClass () {sf. close ();}}
(3) Verification
select * from _teacher;desc _teacher;
Data inserted successfully. id: int (11), primary key, auto_increment
When using Annotation, in addition to the auto policy, you can also use increment, identity, sequence, table, and so on.
Lab 2:
(1) Modify Teacher. java. To change the policy to identity
package com.zgy.hibernate.model;import java.util.Date;import javax.annotation.Generated;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.EnumType;import javax.persistence.Enumerated;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;@Entity@Table(name="_teacher")public class Teacher {private int id;private String name;private String title;private String address;private String wifeName;private Date birth;private ZhiCheng zhiCheng;@Id@GeneratedValue(strategy=GenerationType.IDENTITY)public int getId() {return id;}public void setId(int id) {this.id = id;}@Column(name="_name")public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getWifeName() {return wifeName;}public void setWifeName(String wifeName) {this.wifeName = wifeName;}@Temporal(TemporalType.DATE)public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}@Enumerated(EnumType.STRING)public ZhiCheng getZhiCheng() {return zhiCheng;}public void setZhiCheng(ZhiCheng zhiCheng) {this.zhiCheng = zhiCheng;}}
(2) test
(3) Verification
select * from _teacher;desc _teacher;
Use table policy:
@javax.persistence.TableGenerator(name="Teacher_GEN",table="GENERATOR_TABLE",pkColumnName="pkkey",valueColumnName="pkvalue",pkColumnValue="Teacher",allocationSize=1)
The meaning of the above Annotation: first, a generator is defined. The name of the generator is Teacher_GEN, which generates a table named GENERATOR_TABLE. The table has two fields: pkColumnName and valueColumnName. The first Data in this table is: Teacher, 1. The step value of the next data, that is, the value of the next data in the Teacher table will be 2
Lab 3:
(1) modify the Teacher. java class
package com.zgy.hibernate.model;import java.util.Date;import javax.annotation.Generated;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.EnumType;import javax.persistence.Enumerated;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;@Entity@javax.persistence.TableGenerator(name="Teacher_GEN",table="GENERATOR_TABLE",pkColumnName="pkkey",valueColumnName="pkvalue",pkColumnValue="Teacher",allocationSize=1)public class Teacher {private int id;private String name;private String title;private String address;private String wifeName;private Date birth;private ZhiCheng zhiCheng;@Id@GeneratedValue(strategy=GenerationType.TABLE,generator="Teacher_GEN")public int getId() {return id;}public void setId(int id) {this.id = id;}@Column(name="_name")public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getWifeName() {return wifeName;}public void setWifeName(String wifeName) {this.wifeName = wifeName;}@Temporal(TemporalType.DATE)public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}@Enumerated(EnumType.STRING)public ZhiCheng getZhiCheng() {return zhiCheng;}public void setZhiCheng(ZhiCheng zhiCheng) {this.zhiCheng = zhiCheng;}}
(2) test
(3) Verification
View the SQL statements generated during hibernate execution
Hibernate: select pkvalue from GENERATOR_TABLE where pkkey = 'teacher' for update
Hibernate: insert into GENERATOR_TABLE (pkkey, pkvalue) values ('teacher ',?)
Hibernate: update GENERATOR_TABLE set pkvalue =? Where pkvalue =? And pkkey = 'teacher'
Hibernate: select pkvalue from GENERATOR_TABLE where pkkey = 'teacher' for update
Hibernate: update GENERATOR_TABLE set pkvalue =? Where pkvalue =? And pkkey = 'teacher'
Hibernate: insert into Teacher (address, birth, _ name, title, wifeName, zhiCheng, id) values (?, ?, ?, ?, ?, ?, ?)
View data tables
select * from generator_table;select * from teacher;