标签:
1 @Test 2 public void testTeacherSave() { 3 4 Teacher t = new Teacher(); 5 6 t.setName("t1"); 7 t.setTitle("middle"); 8 t.setBirthDate(new Date()); 9 10 Session session = sessionFactory.openSession(); 11 //Session session = sessionFactory.getCurrentSession(); 12 13 session.beginTransaction(); 14 session.save(t); 15 16 Session session2 = sessionFactory.getCurrentSession(); 17 18 System.out.println(session == session2); 19 20 session.getTransaction().commit(); 21 22 Session session3 = sessionFactory.getCurrentSession(); 23 24 System.out.println(session == session3); 25 26 }
输出:Hibernate:
insert
into
Teacher
(birthDate, gender, good, name, title)
values
(?, ?, ?, ?, ?)
false
false
1 @Test 2 public void testTeacherSave() { 3 4 Teacher t = new Teacher(); 5 6 t.setName("t1"); 7 t.setTitle("middle"); 8 t.setBirthDate(new Date()); 9 10 //Session session = sessionFactory.openSession(); 11 Session session = sessionFactory.getCurrentSession(); 12 13 session.beginTransaction(); 14 session.save(t); 15 16 Session session2 = sessionFactory.getCurrentSession(); 17 18 System.out.println(session == session2); 19 20 session.getTransaction().commit(); 21 22 Session session3 = sessionFactory.getCurrentSession(); 23 24 System.out.println(session == session3); 25 26 }
输出:Hibernate:
insert
into
Teacher
(birthDate, gender, good, name, title)
values
(?, ?, ?, ?, ?)
true
false
Hibernate getCurrentSession()和openSession()的区别
标签:
原文地址:http://www.cnblogs.com/ming-zi/p/5937961.html