1. test to query package COM. example. test; import Java. util. list; import Org. hibernate. hibernateexception; import Org. hibernate. query; import Org. hibernate. transaction; import Org. hibernate. session; import COM. example. bean. people; import COM. example. util. dbutil; public class testquery {@ suppresswarnings ("unchecked") public static void main (string [] ARGs) {@ suppresswarnings ("UNUSED") People = NULL; Session session = d Butil. currentsession (); transaction Tx = NULL; try {Tx = session. begintransaction (); query = session. createquery ("from people"); // the query object is a class peoplelist list = query. list (); For (INT I = 0; I <list. size (); I ++) {People P = (people) list. get (I); system. out. println ("ID:" + P. GETID () + "name:" + P. getname ();} Tx. commit ();} catch (hibernateexception e) {If (TX! = NULL) Tx. rollback (); throw E;} session. Close ();}}
2. test and update package COM. example. test; import Java. util. list; import Org. hibernate. hibernateexception; import Org. hibernate. query; import Org. hibernate. session; import Org. hibernate. transaction; import COM. example. bean. people; import COM. example. util. dbutil; public class testupdate {@ suppresswarnings ("unchecked") public static void main (string [] ARGs) {People P = NULL; Session session = dbutil. currentsession (); transact Ion Tx = NULL; try {Tx = session. begintransaction (); query = session. createquery ("from people where id =: ID"); query. setparameter ("ID", new INTEGER (1); List list = query. list (); If (list. size ()> 0) {P = (people) list. get (0); // system. out. println ("ID:" + P. GETID () + "name:" + P. getname ();} p. setname ("H"); Session. update (p); query = session. createquery ("from people where id =: ID"); query. setparameter ("ID", 1); List = query. List (); If (list. size ()> 0) {P = (people) list. get (0); system. out. println ("ID:" + P. GETID () + "name:" + P. getname () + "status:" + P. getstatus ();} Tx. commit ();} catch (hibernateexception e) {If (TX! = NULL) Tx. rollback (); throw E;} session. Close ();}}
3. insert package COM. example. test; import Java. util. list; import Org. hibernate. hibernateexception; import Org. hibernate. query; import Org. hibernate. session; import Org. hibernate. transaction; import COM. example. bean. people; import COM. example. util. dbutil; public class testinsert {@ suppresswarnings ("unchecked") public static void main (string [] ARGs) {People = NULL; Session session = dbutil. currentsession (); tra Nsaction Tx = NULL; try {Tx = session. begintransaction (); People = new people (); people. setid (6); people. setname ("Xiao Cui"); people. setstatus ("3"); Session. save (people); query = session. createquery ("from people order by ID"); List list = query. list (); If (list. size ()> 0) {People = (people) list. get (list. size ()-1); system. out. println ("new person:" + people. GETID () + "name:" + people. getname () + "status:" + people. getstatus ();} Tx. Commit ();} catch (hibernateexception e) {If (TX! = NULL) {Tx. rollback (); throw e ;}} session. Close ();}}