Hibernate's entity objects have three states, while entity objects are managed by the session:
(1) Transient: The state that was just created, (2) persistent: the status of the session management, the ability to synchronize with the database; (3) Off-tube: The past was managed by the session, but now the session is closed, although there is a record corresponding to the database, but can not be synchronized;
common ways to get started with a session
(1) Query query = session.createquery (HQL): Queries with HQL query statement, (2) Criteria critera = Session.createcriteria (Class clazz); (3) Transaction tx = Session.begintransaction (); Start a transaction; Tx.commit () Submit a transaction; (4) session.close (); The session is closed and then the persisted object managed by the session becomes a de-session.save state; (5) (object obj); Add (6) session.update (Object obj); Update (7) session.delete (Object obj); Delete (8) Object obj = Session.get (Class clazz,serialiazble ID); Finds records based on primary key and returns; (9) Object obj = session.load (Class clazz,serializable ID); The same as the Get method, but lazy loading, that is, he will not return objects until they are used;
Next, we use the session method to create a Hibernateutils class that simplifies the development process in peacetime:
Package org.xiazdong.utils;
Import java.io.Serializable;
Import org.hibernate.HibernateException;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Public final class Hibernateutils {private static sessionfactory factory;
public static Session GetSession () {return factory.opensession (); } private Hibernateutils () {//Singleton mode} static {//Load virtual machine execution once//configure default load Hibernate.cfg.xml//If not hibernate.
Cfg.xml, specify a different name, which is found in the configuration config = new configuration () from Classpath. Configure ();
Factory = Config.buildsessionfactory ();
public static void Add (Object obj) {session session = NULL;
Transaction tx = NULL;
try {session = Hibernateutils.getsession ();
tx = Session.begintransaction ();
Session.save (obj);
Tx.commit ();
} catch (Hibernateexception e) {if (tx! = NULL) {///If there is a transaction, Roll back Tx.rollback (); } throw E; Throws an exception} finally {if (sessIon! = NULL)//If the session is present, close session.close ();
}} public static void update (Object obj) {session session = NULL;
Transaction tx = NULL;
try {session = Hibernateutils.getsession ();
tx = Session.begintransaction ();
Session.update (obj);
Tx.commit ();
} catch (Hibernateexception e) {if (tx! = NULL) {///If there is a transaction, Roll back Tx.rollback (); } throw E;
Throws an exception} finally {if (session! = NULL)//If the session is present, close session.close ();
}} public static void Delete (Object obj) {session session = NULL;
Transaction tx = NULL;
try {session = Hibernateutils.getsession ();
tx = Session.begintransaction ();
Session.delete (obj);
Tx.commit ();
} catch (Hibernateexception e) {if (tx! = NULL) {///If there is a transaction, Roll back Tx.rollback (); } throw E;
Throws an exception} finally {if (session! = NULL)//If the session is present, close session.close ();
}} public static Object get (Class clazz, Serializable ID) {session session = NULL; try {session= Hibernateutils.getsession ();
Object obj = Session.get (clazz, id);
return obj;
} finally {if (session! = NULL) {session.close ();
}
}
}
}
Test class:
Package Org.xiazdong;
Import Org.xiazdong.utils.HibernateUtils;
public class Usertest {public
static void Main (string[] args) {
user U = new User ();
U.setname ("Xiazdong-1");
U.setage (a);
Hibernateutils.add (u); Use the Add method
//This statement
System.out.println ("Join successfully ...");
U = (User) hibernateutils.get (user.class,1); Use the Get method
System.out.println ("Get Name:" +u.getname ());
}
}