Hibernate framework study notes 4: Primary Key Generation Policy, object status, hibernate Study Notes
Create an object class:
package domain;public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]"; }}View Code
ORM metadata configuration file:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <! -- Relationship between table and object -->
The primary key generation policy here is native.
Details:
The primary key generation policy is the primary key generation rule when each record is input. (7)
Identity: auto-increment primary key. The database maintains the primary key value. The primary key is not required during input.
Sequence: The primary key generation policy in Oracle. MySQL is used here.
Increment (not used, with low efficiency and thread security issues): The primary key auto-increment is maintained by hibernate. The maximum id value of the table is queried before each insert. + 1 is used as the new primary key value.
Hilo (don't use it, you just need to know): high/low bit algorithm, primary key auto-increment. It is maintained by hibernate and not used during development.
Native: hilo + sequence + identity automatically selects one policy (checks the current database and uses the corresponding policy)
Uuid: generate a random string that never repeats theoretically as the primary key. The primary key type must be string.
Assigned: Policy for generating natural primary keys. Hibernate does not manage primary key values, which are input by developers themselves.
Hibernate objects are divided into three states:
Instantaneous status: No id, no session Association
Persistence status: Id, associated with session
Free Status(Managed status): Has an id and is not associated with the session.
Package state; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. junit. test; import domain. customer; import utils. hibernateUtils; // three States of the Test object: public class Demo {@ Test // view three states: public void fun1 () {// 1 get session Session session = HibernateUtils. openSession (); // 2 controls the Transaction tx = session. beginTransaction (); // 3 execute the operation Customer c = new Customer (); // No id, not associated with session => Instantaneous State c. setCust_name ("Lenovo"); // instantaneous session. save (c); // Persistence State, with id and association // 4 commit transactions. disable resource tx. commit (); session. close (); // free | hosting status, with id, no Association} @ Test // three States features // save method: Actually, it cannot be understood as saving. to convert an object to a persistent state, convert the instantaneous state to a persistent state. // The primary key auto-increment function is used to convert the object to a persistent state when the save method is executed. id value must be generated. therefore, you need to execute the insert statement to generate. // if the primary key generation policy is increment: Execute the save method to generate an id. the SQL statement (select max (id) from Table) that queries the maximum id is executed ). public void fun2 () {// 1 get session Session session = HibernateUtils. openSession (); // 2 controls the Transaction tx = session. beginTransaction (); // 3 execute the operation Customer c = new Customer (); // No id, not associated with session => Instantaneous State c. setCust_name ("Lenovo"); // instantaneous session. save (c); // Persistence State, with id and association // 4 commit transactions. disable resource tx. commit (); session. close (); // free | hosting status, with id, no Association} @ Test // three States features // persistence status features: any changes to the persistent State object will be automatically synchronized to the database. public void fun3 () {// 1 get session Session session = HibernateUtils. openSession (); // 2 controls the Transaction tx = session. beginTransaction (); // 3. execute the operation Customer c = session. get (Customer. class, 1l); // Persistence State object c. setCust_name ("Microsoft"); // 4 submit the transaction. disable resource tx. commit (); session. close (); // free | hosting status, with id, no Association }}
Next, we will analyze the addition, deletion, modification, and query of hibernate from the perspective of status: