1. Writing entity class person
Package Com.icss.pojo;public class Person {private int uid;private string Uname;private string Pword;private string addr;p ublic int Getuid () {return UID;} public void SetUid (int uid) {this.uid = uid;} Public String Getuname () {return uname;} public void Setuname (String uname) {this.uname = uname;} Public String Getpword () {return pword;} public void Setpword (String pword) {This.pword = Pword;} Public String getaddr () {return addr;} public void setaddr (String addr) {this.addr = addr;} @Overridepublic String toString () {return "person [uid=" + uid + ", uname=" + uname + ", pword=" + pword+ ", addr=" + addr + "]";}}
2, Write Person.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 ">
3, Write Hibernate.cfg.xml
<?xml version= ' 1.0 ' encoding= ' UTF-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hi Bernate.org/dtd/hibernate-configuration-3.0.dtd "><!--Generated by MyEclipse hibernate Tools. -->4. Writing tool Classes
Package Com.icss.util;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public class hibernateutils {private static sessionfactory SF = Null;private hibernateutils () {//TODO auto-generated constructor stub} public static Sessionfactory Getsessionfactory () {if (sf==null) {configuration cfg=new configuration (). Configure (); sf= Cfg.buildsessionfactory ();} return SF;}}
5, write Hibernate's additions and deletions to check the test
Package Com.icss.test;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.junit.ignore;import org.junit.Test; Import Com.icss.pojo.person;import Com.icss.util.hibernateutils;public class Hibernatecrudcache {/** * crud operations * solve Chinese problems first , specify the encoding type when configuring the data connection, note & * <property name= "Hibernate.connection.url" > * jdbc:mysql://localhost:3306/ Hibernate_day01?useunicode=true&characterencoding=utf8 * </property> * * @Testpublic void test1 () { Configuration cfg = new configuration (). Configure ("Hibernate.cfg.xml"); Sessionfactory sf=cfg.buildsessionfactory (); Session ss = Sf.opensession ();//Open transaction Transaction tx = Ss.begintransaction (); /** * Add * person p=new person (' zs ', ' 123 ', ' Shanghai '); * Ss.save (P); * When added, if an already existing ID is set, save will automatically handle the ID problem, adding a record (ID increment) *//** * query * Use the reflection mechanism to specify the specific class and primary key to find the corresponding data record * Person object = (person ) Ss.get (Person.class, 10); * SYSTEM.OUT.PRINTLN (object); *//** * Modify * First querySpecify the object to modify, and then modify the * modify process, into the person table, locate the specified object based on the ID, and then execute the UPDATE statement to complete the modification * person p= (person) ss.get (person.class,13); * P.setuname ("John Doe"); * Ss.update (P); * If the change, the ID is not query out, the specified, you want to give an accurate ID, and modify, all the properties to be set, otherwise null *//** * Delete * First query specifies the object to be modified, and then delete the * person P = (person) ss.get (person . class,10); * Ss.delete (P); *///first-level cache authentication/** System.out.println ("--------------------"); Person P = (person) ss.get (person.class,11); SYSTEM.OUT.PRINTLN (P); System.out.println ("--------------------"); person P2 = (person) ss.get (person.class,11); System.out.println (p2); *///first-level cache attribute Person P = (person) ss.get (person.class,11);p. Setuname ("Zhang San");p. Setpword ("000"); P.SETADDR ("Shanghai"); Tx.commit (); Ss.close (); Sf.close ();}} /** * Entity Class object State (concept) There are three kinds of transient states: There is no ID in the object, the object is not associated with the session, it is generally added operation * person p=new person (' zs ', ' 123 ', ' Shanghai '); * Ss.save (P); * * Persistent state: Object has ID, object is associated with session query operation * Person P = (person) ss.get (person.class,10); * * Managed state: The object has an ID, but the object and the session are not associated with the object they created * the difference between transient and managed states: transient states do not specify an ID value, the managed state specifies the ID, but all create the object by itself * Note: When you specify an ID value for an object that you create, save is added Add operation, and Saveorupdates is the modification operation! * * in transient state, saveorupdate is doing insert operation * in persistent state, Saveorupdate is doing update operation * in Managed state, Saveorupdate is doing update operation */
6, writing hibernate transactions testing
Package Com.icss.test;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.junit.test;import Com.icss.pojo.person;import com.icss.util.HibernateUtils; public class Hibernatetransaction {@Testpublic void test1 () {sessionfactory SF = null; Session SS = null; Transaction tx = null;try {SF = Hibernateutils.getsessionfactory ();//Use Sessionfactory to create session object SS = Sf.opensession () ;//Open Transaction tx = Ss.begintransaction ();//Add function person p = new person ();p. Setuname ("Harry");p. Setpword ("111");p. Setaddr ("Huangpu"); /Call the implementation of the session object to complete adding Ss.save (p);//Simulate an exception int x = 10/0;//COMMIT Transaction Tx.commit ();} catch (Exception e) {//Output exception information//e.printstacktrace ();//If there is an exception, ROLLBACK TRANSACTION/** * The cause of the transaction rollback failure is the reason for the database, * MySQL database only InnoDB engine support transaction; * Default citation Dynasky is MyISAM, does not support transactions, * So, need to set the database table structure for INNODB; * That is, ALTER TABLE name ENGINE=INNODB; * At the same time, the dialect is set to Mysqlinnodbdialect */if (tx! = null) {ss.clear (); Tx.rollback (); SYSTEM.OUT.PRINTLN ("Transaction rollback! ");}} Finally {//close resource if (ss! = null && ss.isopen ()) {ss.close ()}; if (SF ! = null &&!sf.isclosed ()) {sf.close ();}}}}
Hibernate Day2 Case Code