Basic concepts and curdDevelopment process1 by domain object, mapping->db. (Official recommendation)2 starts with DB and uses tools to generate mapping and domain object. (use more) 3 starts with the mapping file.
Domain Object Restriction1. The default construction method (required). 2 meaningless identifier ID (primary key) (optional) 3 non-final, has an effect on lazy loading (optional) Domain Java Object (User)
Public class User { privateint ID; Private String name; Private Date BirthDay; // getter setter ...}
Java
Code
1.
Initialize Code
(
just once.
)
New Configuration (); Cfg.configure ("Config.cfg.xml"); // You can also set properties through Cfg.setproperty. sessionfactory sessionfactory = Cfg.buildsessionfactory ();
So encapsulate the above code as a tool class:
Hibernateutils.java
Packagecom.dzq.utils;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration; Public classHibernateuntils {Private Staticsessionfactory sessionfactory; Privatehibernateuntils () {}Static{Configuration cfg=NewConfiguration (); Cfg.configure ();//If you do not hibernate.cfg.xml This file name, you need to add the filenameSessionfactory =cfg.buildsessionfactory (); } Public Staticsessionfactory getsessionfactory () {returnsessionfactory; } Public StaticSession getsession () {returnsessionfactory.opensession (); }}
Use of the tool class:
Public Static voidaddUser (user user) {Session s=NULL; Transaction TX=NULL; Try{s=hibernateuntils.getsession (); TX=s.begintransaction (); S.save (user); Tx.commit (); } Catch(Exception e) {if(tx!=NULL) Tx.rollback (); Throw NewRuntimeException (e); }finally{ if(s!=NULL) {s.close (); } } }
Several main methods of the session1.save,persist saves the data, persist does not produce an INSERT statement outside the transaction. 2.delete, delete object 3.update, update object, if there is no record in the database, an exception will occur. 4.get, according to the ID, will immediately access the database. 5.Load, based on ID, (returns the agent, does not immediately access the database). 6.saveorupdate,merge (based on the value of ID and version to determine whether it is save or update), call merge your object or managed. 7.lock (turns the object into a persistent object, but does not synchronize the state of the object).
Object StateInstantaneous (transient): There is no data in the database corresponding to it, more than the scope will be reclaimed by the JVM garbage collector, usually new and not associated with the session object. Persistent (persistent): There is data in the database corresponding to it, currently associated with the session, and the associated session is not closed, the transaction is not submitted;
Persistent object state changes in the event
the database is affected when the service is submitted
(Hibernate
can detect
)
. Off-pipe (detached): There is data in the database corresponding to it, but there is no session associated with it; The managed object state has changed and hibernate cannot detect it.
tools to improve:
Hibernateutils.java
Packagecom.dzq.utils;Importjava.io.Serializable;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration; Public classHibernateuntils {Private Staticsessionfactory sessionfactory; Privatehibernateuntils () {}Static{Configuration cfg=NewConfiguration (); Cfg.configure ();//If you do not hibernate.cfg.xml This file name, you need to add the filenameSessionfactory =cfg.buildsessionfactory (); } Public Staticsessionfactory getsessionfactory () {returnsessionfactory; } Public StaticSession getsession () {returnsessionfactory.opensession (); } /*** Add *@paramEntity*/ Public Static voidAdd (Object entity) {Session S=NULL; Transaction TX=NULL; Try{s=hibernateuntils.getsession (); TX=s.begintransaction (); S.save (entity); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) Tx.rollback (); Throw NewRuntimeException (e); } finally { if(s! =NULL) {s.close (); } } } /*** Change *@paramEntity*/ Public Static voidUpdate (Object entity) {Session S=NULL; Transaction TX=NULL; Try{s=hibernateuntils.getsession (); TX=s.begintransaction (); S.update (entity); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) Tx.rollback (); Throw NewRuntimeException (e); } finally { if(s! =NULL) {s.close (); } } } /*** Delete *@paramEntity*/ Public Static voidDelete (Object entity) {Session S=NULL; Transaction TX=NULL; Try{s=hibernateuntils.getsession (); TX=s.begintransaction (); S.delete (entity); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) Tx.rollback (); Throw NewRuntimeException (e); } finally { if(s! =NULL) {s.close (); } } } /*** Query based on PRIMARY key ID *@paramClazz *@paramID *@return */ Public StaticObject Get (Class clazz,serializable ID) {Session s=NULL; Try{s=hibernateuntils.getsession (); Object obj=S.get (clazz, id); returnobj; } finally { if(s! =NULL) {s.close (); } } }}
20160505-hibernate Getting Started 2