Hibernate---Three states of an object
In short, Hibernate is an object-oriented ORM-based framework that is located in the DAO layer and is a framework for manipulating data. I'll talk about the three states of Hibernate objects. They were: free, persistent and instantaneous. Use the code to explain it.
Hibernate.cgf.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibern Ate.org/dtd/hibernate-configuration-3.0.dtd "> User.hbm.xml
<?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 "><!--ORM Metadata Table Object Relational mapping file package: Configure the packages in this configuration file that are in the same class. -->User
Package com.huhu.domain;//Hibernatepublic class User { private Integer ID; private String name; private String password; Public Integer getId () { return ID; } public void SetId (Integer id) { this.id = ID; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public String GetPassword () { return password; } public void SetPassword (String password) { this.password = password; } @Override public String toString () { return "User [id=" + ID + ", name=" + name + ", password=" + Password + "]"; }}
Hibernateutils
package Com.huhu.utils;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import org.hibernate.classic.session;//Complete Hibernate tool class//package profile read operation// Package Sessionfactroy Create operation//encapsulation session get Operation public class Hibernateutils {private static sessionfactory sf;static{// 1 Load config Configuration conf = new configuration (). Configure ();//2 create SESSIONFACTORYSF based on configuration information Conf.buildsessionfactory ();//runtime.getruntime (). Addshutdownhook (New Thread (new Runnable () {@Overridepublic void Run () {System.out.println ("virtual institution closed! Release resources"); Sf.close ();}));} public static Org.hibernate.Session Opensession () {//3 gets sessionsession session = Sf.opensession (); return session;} public static Org.hibernate.Session Getcurrentsession () {//3 gets sessionsession Session = Sf.getcurrentsession (); return session;} public static void Main (string[] args) {System.out.println (opensession ());}}
HibernateDemo1
Package Com.huhu.a_state;import Com.huhu.domain.user;import Com.huhu.utils.hibernateutils;import org.hibernate.session;/** * Three states of the object */public class HibernateDemo1 {//three states public void Fun1 () {Session Sessi On = Hibernateutils.opensession (); Session.begintransaction (); --------------------------------------User U = new user (); Status: Transient state u.setname ("Tom"); Status: Instantaneous state U.setpassword ("1234"); Status: Transient state//Save Session.save (U); Status: Persistent state//When the Save method is called, the database has no corresponding record//no record, but will eventually be synchronized to the database, still persistent state//--------------------------------- -----session.gettransaction (). commit (); Status: Persistent state session.close (); Status: Free State}//conversion of three states instantaneous = "Persistent public void Fun2 () {Session session = Hibernateutils.opensession (); Session.begintransaction (); --------------------------------------User U = new user (); Status: Instantaneous state u.setname ("he"); Status: Instantaneous state U.setpassword ("123456"); Status: Instantaneous state session.save (U); Status: Persistent state//--------------------------------------session.gettransaction (). commit (); Status: Persistent state//When a transaction commits, the persisted object is saved to the database//increment=>hibernate automatically queries the maximum ID, then generates the primary key//assigned = = automatically generates the primary key Session.close (); }//instantaneous = "Free instantaneous: no association No ID free: no correlation with ID (id corresponding to database) public void Fun3 () {Session session = Hibernateutils.opensessi On (); Session.begintransaction (); --------------------------------------//status: Persistent state User u = New User (); U.setid (2); This is the Free State//--------------------------------------session.gettransaction (). commit (); Status: Persistent state//The persisted object is saved to the database Session.close () when the transaction commits; }//persistent = "Instantaneous 1 persistent: associated, with ID, instantaneous: No association without ID public void Fun4 () {Session session = Hibernateutils.opensession (); Session.begintransaction (); --------------------------------------//status: Persistent state//through the Get method, get the persistent state object user U = (user) session.get (user). class, 2); Persistent state//--------------------------------------session.gettransaction (). commit (); The persisted object is saved to the database Session.close () when the transaction commits; Free State u.setid (NULL); }//persistent = "Instantaneous 1 persistent: associated, with ID, instantaneous: No association without ID public void Fun5 () {Session session = Hibernateutils.opensession (); Session.begintransaction (); --------------------------------------//status: Persistent state//through the Get method, get the persistent state object user U = (user) session.get (user). class, 4); Persistent state session.evict (U); Removes the U.setid (NULL) from the association of the user object and the session; Instantaneous object//--------------------------------------session.gettransaction (). commit (); Session.save (U); The persisted object is saved to the database Session.close () when the transaction commits; Free State}//persistent = "free persistent: associated with id public void Fun6 () {Session session = HiberNateutils.opensession (); Session.begintransaction (); --------------------------------------//status: Persistent state//through the Get method, get the persistent state object user U = (user) session.get (user). class, 4); Persistent state session.evict (U); Removes the association of the user object from the session//--------------------------------------session.gettransaction (). commit (); Free Session.close (); Free State}//free = "instantaneous instantaneous: no association without ID public void Fun7 () {Session session = Hibernateutils.opensession (); Session.begintransaction (); --------------------------------------//status: Persistent state//through the Get method, get the persistent state object user U = (user) session.get (user). class, 4); Persistent state session.evict (U); Instantaneous u.setid (null); Instantaneous//--------------------------------------session.gettransaction (). commit (); Instantaneous session.close (); Transient state}//free = "is persistent associated with the session public void Fun8 () {Session session = Hibernateutils.opensession (); SeSsion.begintransaction (); --------------------------------------//status: Persistent state//through the Get method, get the persistent state object user U = (user) session.get (user). class, 4); Persistent state session.evict (U); Free session.update (u); Durable//--------------------------------------session.gettransaction (). commit (); Persistent Print Updata statement session.close (); Transient state} public void Fun9 () {Session session = Hibernateutils.opensession (); Session.begintransaction (); --------------------------------------////through the Get method, get the persistent state object User u = (user) Session.get (User.class, 4); U.setname ("Nenen"); --------------------------------------session.gettransaction (). commit (); Session.close (); Transient state} public static void Main (string[] args) {HibernateDemo1 d = new HibernateDemo1 (); D.fun1 (); D.fun9 (); }}
The code is obvious. If you don't think it's obvious, take another picture.
Let's summarize the following:
What is the use of three states:
1. Persistent state: We use hibernate primarily to persist data
2. For the state of the object: we expect that the data that we now need to synchronize to the database is converted to persistent
Features of persistent speech objects:
Hibernate automatically synchronizes changes to persistent session objects to the database.
So hibernate is an object-oriented framework that leverages the object's operational data.
Hibernate---Three states of an object