ONLY14
2016-06-11 10:58
@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(); }
我将你的代码运行了一遍,你当中少了我添加的那条 3.获取sessionFactory对象 然后运行结果的hashcode值是相同的。
为什么不同呢?
上面的生成的两次session所对应的connection的hashcode是不一样的
Hibernate初探之单表映射
74810 学习 · 793 问题
相似问题