A tentative study on Hibernate course single-table mapping 2-4 Transaction Introduction

Source: Internet
Author: User

1 Hibernate is non-auto-commit. If transaction does not write, only the table structure is created and no statements are inserted.

If you do not write transaction and want to implement the insertion function, you need to rewrite the session DoWork method (remember flush).

2 Demo:

Package hibernate_001;

Import Java.sql.Array; Import Java.sql.Blob; Import java.sql.CallableStatement; Import Java.sql.Clob; Import Java.sql.DatabaseMetaData; Import Java.sql.NClob; Import java.sql.PreparedStatement; Import java.sql.SQLClientInfoException; Import java.sql.SQLException; Import java.sql.SQLWarning; Import Java.sql.SQLXML; Import Java.sql.Savepoint; Import java.sql.Statement; Import java.sql.Struct; Import Java.util.Date; Import Java.util.Map; Import java.util.Properties; Import Java.util.TimeZone; Import Java.util.concurrent.Executor;

Import org.hibernate.Session; Import Org.hibernate.SessionFactory; Import org.hibernate.Transaction; Import org.hibernate.cfg.Configuration; Import Org.hibernate.jdbc.Work; Import Org.hibernate.service.ServiceRegistry; Import Org.hibernate.service.ServiceRegistryBuilder; Import Org.junit.After; Import Org.junit.Before; Import Org.junit.Test;

Import com.ddwei.student.Student; import com.mysql.jdbc.Connection; import com.mysql.jdbc.ExceptionInterceptor; Import com.mysql.jdbc.Extension; Import com.mysql.jdbc.MySQLConnection; Import Com.mysql.jdbc.log.Log;

public class Studenttest {   private sessionfactory sessionfactory;  private session session;  priva Te Transaction trasaction;     @Test  public void Testsavestudent () {  student Student =new Student (3, "Zhou Enlai", "male", New Date () , "Shaoxing");//Create Student Object   session.save (student);//Session Save student object into database        }    @ Before  public void Init () {  //1   Create config object   configuration config = new configuration (). Configure ();   //2   Create service object   serviceregistry Servicere = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry ();      //3   Create session Factory   sessionfactory = Config.buildsessionfactory (Servicere);      //4   Open Session   session = Sessionfactory.opensession ();   //5   Create Transaction//  trasaction = Session.begintransaction ();  }       @After  public void Destroy () {//  trasaction.commit ();   session.dowork (new work () {& nbsp;       @Override    public void execute (java.sql.Connection arg0) throws SQLException {    //TODO auto-generated method Stub     arg0.setautocommit ( true);         }   });   session.flush ();   session.close ();   sessionfactory.close ();  }  

}

A tentative study on Hibernate course single-table mapping 2-4 Transaction Introduction

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.