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