@Test
public void testWithGetCurrentSession() {
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.getCurrentSession();
Transaction transaction1 = session.beginTransaction();
Students s = new Students(3, "张三", "男", new Date(), "北京");
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.println("connection hashcode:"
+ connection.hashCode());
}
});
session.save(s);
// session1.close();
transaction1.commit();
Session session2 = sessionFactory.getCurrentSession();
transaction1 = session2.beginTransaction(); // 生成学生对象
s = new Students(4, "类似", "男", new Date(), "大海");
session2.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.println("connection hashcode:"
+ connection.hashCode());
}
});
session2.save(s);
transaction1.commit();
}