Hibernate 2. CRUD
Key Generator (primary Key Generator)
Option description:
1) assigned
The primary key is generated by an external program and does not involve Hibernate.
2) hilo
The primary key generation mechanism implemented by the hi/lo algorithm requires additional database tables to save the Historical Status of the primary key generation.
3) seqhilo
Similar to hilo, the primary key generation mechanism implemented through the hi/lo algorithm only stores the Historical Status of the primary key in Sequence, which is suitable for databases that support Sequence, such as Oracle.
4) increment
The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated.
This method may cause a problem: if multiple instances access the same database, different instances may generate the same primary key because each instance maintains its primary key status, this causes duplicate primary key exceptions. Therefore, if the same database has multiple instances to access, this method must be avoided.
5) identity
Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.
6) sequence
Use the sequence mechanism provided by the database to generate the primary key. For example, Sequence in Oralce.
7) native
Hibernate uses identity, hilo, and sequence as the primary key generation method based on the underlying database.
8) uuid. hex
The algorithm generated by Hibernate Based on the 128-bit unique value generates a hexadecimal value (encoded with a 32-Bit String) as the primary key.
9) uuid. string
Similar to uuid. hex, the generated primary key is not encoded (Length: 16 ). Problems may occur in some databases (such as PostgreSQL ).
10) foreign
Use the field of the External table as the primary key. Generally, using uuid. hex to generate a primary key provides the best performance and database platform adaptability.
Ing file description:
Package: namespace of the class
Class: class (entity class) corresponding to the Database class)
Id: Master Region
Name: attributes of the object class
Column: primary key field of the table
Generator: primary key generator
Class: the type of primary key generation. For more information, see the option description of the primary key generator above.
Property: Properties of the object class
Name: attributes of the object class
Column: primary key field of the table
Tip:
If the name is the same as the column name, you do not need to set column, for example, age.
CRUD:
Package com. demo. model; import java. util. list; import org. hibernate. hibernateException; import org. hibernate. query; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import org. junit. before; import org. junit. test; import com. demo. model. student;/*** @ author wobendiankun * 08:54:13 */public class StudentTest {SessionFactory sessionFactory; @ Beforepublic void init () {sessionFactory = new Configuration (). configure (). buildSessionFactory () ;}@ Testpublic void addTest () {Student student = new Student (); student. setAge (20); student. setStudentName ("wangcai"); Session session = null; try {// enable sessionsession = sessionFactory. openSession (); // start the transaction session. beginTransaction (); session. save (student); // submit the transaction session. getTransaction (). commit ();} catch (HibernateException e) {E. printStackTrace (); // rollback transaction session. getTransaction (). rollback ();} finally {if (session! = Null) {session. close () ;}}@ Testpublic void deleteTest () {Student student = new Student (); student. setStudentId (1); Session session = null; try {session = sessionFactory. openSession (); session. beginTransaction (); session. delete (student); session. getTransaction (). commit ();} catch (HibernateException e) {e. printStackTrace (); session. getTransaction (). rollback ();} finally {if (session! = Null) {session. close () ;}}@ Testpublic void updateTest () {Student student = new Student (); student. setStudentId (2); student. setAge (35); Session session = null; try {session = sessionFactory. openSession (); session. beginTransaction (); // Note: The update method updates all fields except the primary key of t_student. // student_name is not set. The default value is null, // The data in the table will be updated to a null session. update (student); // update t_student set student_name = ?, Age =? Where student_id =? Session. getTransaction (). commit ();} catch (HibernateException e) {e. printStackTrace (); session. getTransaction (). rollback ();} finally {if (session! = Null) {session. close () ;}}@ Testpublic void getTest () {Session session = null; Student student = null; try {session = sessionFactory. openSession (); student = (Student) session. get (Student. class, 2);} finally {if (session! = Null) {session. close () ;}} System. out. println (student) ;}@ Testpublic void listTest () {Session session = null; List
List = null; try {session = sessionFactory. openSession (); Query query = session. createQuery ("from Student"); list = query. list ();} finally {if (session! = Null) {session. close () ;}for (Student student: list) {System. out. println (student );}}}