Some methods of Hibernate basic crud
CRUD is the abbreviation for Create, READ, UPDATE, delete
The methods for inserting records in 1.Hibernate are:
Session.save ();
Session.persist ();
The methods for querying records in 2.Hibernate are:
Session.get ()
Session.load ();
The methods for updating records in 3.Hibernate are:
Session.update ();
Session.updateorsave ();
Session.merge ();
The methods for deleting records in 4.Hibernate are:
Session.delete ();
Instance
The code is as follows |
Copy Code |
Package Org. Rudiment.hibernate; Import Java.util.Iterator; Import java.util.List; Import Org.hibernate.Query; Import org.hibernate.Session; Import Org.hibernate.SessionFactory; Import org.hibernate.Transaction; Import org.hibernate.cfg.Configuration; Import Org.hibernate.service.ServiceRegistry; Import Org.hibernate.service.ServiceRegistryBuilder; public class Hibernatehandler {
/* Insert new data * * public static void Insertobejct (News N) { Create a Configuration object Configuration conf = new Configuration (); serviceregistry sr = new Serviceregistrybuilder () . Applysettings (Conf.getproperties ()) . Buildserviceregistry (); Get sessionfactory from Configuration object Sessionfactory SF = Conf.configure (). Buildsessionfactory (SR); Get to a session via Sessionfactory Session Sess = Sf.opensession (); Session that starts the sessions transaction Transaction tx = Sess.begintransaction (); Set the title of this instance N.settitle ("Hibernate frame 1"); Set the content of this instance N.setcontent ("Hibernate frame inserted content 2"); Save Instance Sess.save (n); Commit a transaction Tx.commit (); Close session Sess.close (); Close Session Factory Sf.close (); }
* * Query Record * * Public News Querybyid (int id) { Configuration conf = new Configuration (); serviceregistry sr = new Serviceregistrybuilder () . Applysettings (Conf.getproperties ()) . Buildserviceregistry (); Sessionfactory SF = Conf.configure (). Buildsessionfactory (SR); Session Sess = Sf.opensession (); String hql = "from News as N where n.id=?"; Query query = sess.createquery (HQL); Query.setinteger (0, id); List rslist = Query.list (); Iterator It=rslist.iterator (); News = null; while (It.hasnext ()) { News = (News) it.next (); } Sess.close ();
return news; }
/* Update Record * * Public News Updatebyid (int id) { News n = Querybyid (ID); Configuration conf = new Configuration (); serviceregistry sr = new Serviceregistrybuilder () . Applysettings (Conf.getproperties ()) . Buildserviceregistry (); Sessionfactory SF = Conf.configure (). Buildsessionfactory (SR); Session Sess = Sf.opensession (); Transaction t = sess.begintransaction (); N.settitle ("Modified, I am" + ID); N.setcontent ("content also changed" + ID + "!"); Sess.update (n); T.commit (); Sess.close (); return Querybyid (ID); }
/* Delete Record * * public boolean Deletebyid (int id) { Configuration conf = new Configuration (); serviceregistry sr = new Serviceregistrybuilder () . Applysettings (Conf.getproperties ()) . Buildserviceregistry (); Sessionfactory SF = Conf.configure (). Buildsessionfactory (SR); Session Sess = Sf.opensession (); String hql = "Delete News as n where n=?"; Query query = sess.createquery (HQL); Query.setinteger (0, id); Query.executeupdate (); Sess.begintransaction (). commit ();
return true; }
public static void Main (string[] args) { Long p_t = System.currenttimemillis (); for (int i=0; i<10; i++) { News n = new News (); HIBERNATEHANDLER.INSERTOBEJCT (n); } Long l_t = System.currenttimemillis (); Long usetime = (l_t-p_t)/1000; SYSTEM.OUT.PRINTLN ("Time Consuming:" + (Usetime) + "seconds"); } } |