Hibernate basic CRUD Operations Instance (update, save, delete, query)

Source: Internet
Author: User
Tags commit

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");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.