Hibernate's Gettransaction () and BeginTransaction ()

Source: Internet
Author: User

Session.gettransaction () only gets a transaction instance based on the session, but does not start it

Session.begintransaction () calls its begin () method after acquiring a transaction

(1)

Session = Hibernateutil.getsession ();//This is the operation to open the session
Session.begintransaction ();//This is the operation to turn on transaction
Session.save (user);//This is the operation of putting data into a first-level cache session
Session.gettransaction (). commit ();//Here is the statement that really interacts with the database

(2)

Session session=hibernatetools.getsession ();
Transaction tx=session.begintransaction ();

Session.save (user);

Tx.commit ();
Session.close ();

Such as:

public static void Main (string[] args) throws ParseException {
Get configuration file
Configuration configuration = new configuration (). Configure ();
Registering a Service object
Standardserviceregistrybuilder Serviceregistrybuilder = new Standardserviceregistrybuilder ();
Standardserviceregistry serviceregistry = serviceregistrybuilder.applysettings (Configuration.getproperties ()). Build ();
Get Sessionfactory
Sessionfactory sessionfactory = configuration.buildsessionfactory (serviceregistry);
Get session
Session session = Sessionfactory.opensession ();
Open transaction
Session.begintransaction ();
Start Operation *************//
1. Get the user data with ID 1 using the Get method
Commodity getcommodity = (commodity) session.get (Commodity.class, 1);
System.out.println ("Date Created:" + getcommodity.getcreatedate () + "description:" + getcommodity.getdescribe () + "Date Modified:" + Getcommodity.getmodifydate () + "name:" + getcommodity.getname ());

Date date = new Date (0);
String stringdate = "2019-05-07";
Date date = new SimpleDateFormat ("Yyyy-mm-dd"). Parse (stringdate);

2. Adding goods
Commodity newcommodity = new Commodity (date, "Electrical 1", date, "Rice cooker 2");
Session.save (newcommodity);

3. By deleting
Commodity getCommodity1 = (commodity) session.load (Commodity.class, 2);
Session.delete (getCommodity1);
Session.gettransaction (). commit ();

4. Change
Commodity getCommodity2 = (commodity) session.get (Commodity.class, 1);
Getcommodity2.setname ("wash basin");
Session.update (GetCommodity2);
Session.gettransaction (). commit ();
End Operation *************//


Commit a transaction
Session.gettransaction ();
Close session
Session.close ();
Close Sessionfactory
Sessionfactory.close ();
}

Again such as:

Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import Org.hibernate.boot.registry.StandardServiceRegistry;
Import Org.hibernate.boot.registry.StandardServiceRegistryBuilder;
Import org.hibernate.cfg.Configuration;

Import Online.shixun.shop.model.User;

public class Hibernatedemo {

public static void Main (string[] args) {
Get configuration file
Configuration configuration = new configuration (). Configure ();
Registering a Service object
Standardserviceregistrybuilder Serviceregistrybuilder = new Standardserviceregistrybuilder ();
Standardserviceregistry serviceregistry = serviceregistrybuilder.applysettings (Configuration.getproperties ()). Build ();
Get Sessionfactory
Sessionfactory sessionfactory = configuration.buildsessionfactory (serviceregistry);
Get Session
Session session = Sessionfactory.opensession ();
Open transaction
Session.begintransaction ();

Start Data Manipulation ***************//

1. Get the user data with ID 1 using the Get method
User GetUser = (user) Session.get (user.class, 1);
System.out.println ("Get username/password/age:" + getuser.getname () + "/" + Getuser.getpassword () + "/" + getuser.getage ());

2. Use the Load method to get user data with ID 1
User Loaduser = (user) session.load (user.class, 1);
System.out.println ("Load username/password/age:" + loaduser.getname () + "/" + Loaduser.getpassword () + "/" + loaduser.getage ());

3. Use the Save method to add user data (execute the following statement and use the Database client tool to query the table for new data additions)
User NewUser = New User ("Wuhan", "Innovaee", 16);
Session.save (NewUser);

4. Modify GetUser data using the Saveorupdate method
Getuser.setname ("Light practical training! ");
Session.saveorupdate (GetUser);

5. Delete getUser data using the Delete method
Session.delete (GetUser);

End data Operation ***************//

Commit a transaction
Session.gettransaction (). commit ();
Close Sessin
Session.close ();
Close Sessionfactory
Sessionfactory.close ();
}
}

Hibernate's Gettransaction () and BeginTransaction ()

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.