package junit;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.junit.Test;
import com.model.Student;
public class SessionTest {
@Test
public void test1(){
Configuration configure = new Configuration().configure();
SessionFactory sessionFactory =configure.buildSessionFactory();
Session session = sessionFactory.openSession();
//System.out.println(session==session2);
Transaction trans=session.beginTransaction();
session.doWork(new Work() {
@Override
public void execute(Connection arg0) throws SQLException {
// TODO Auto-generated method stub
System.out.println(arg0.hashCode());
}
});
Student s = new Student(7, "kkk", new Date(), "上海");
session.save(s);
trans.commit();
Session session2 = sessionFactory.openSession();
trans=session2.beginTransaction();
session2.doWork(new Work() {
@Override
public void execute(Connection arg0) throws SQLException {
// TODO Auto-generated method stub
System.out.println(arg0.hashCode());
}
});
s = new Student(6, "111", new Date(), "上海");
session2.save(s);
trans.commit();
session.close();
session2.close();
}
}