_神人A
2016-08-05 00:08
//获得配置对象 Configuration config = new Configuration().configure(); //获得服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //获得sessionFactory对象 SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); //创建session对象 Session session1 = sessionFactory.getCurrentSession(); //开启事务 Transaction transaction = session1.beginTransaction(); //生成一个学生对象 Student s = new Student(1,"张","nan",new Date(),"北京"); session1.doWork(new Work(){ @Override public void execute(Connection connection) throws SQLException { System.out.println("getCurrentSession1 connection hashCode" + connection.hashCode()); } }); session1.save(s); //session1.close(); transaction.commit(); Session session2 = sessionFactory.getCurrentSession(); transaction = session2.beginTransaction(); s = new Student(2,"张34","男",new Date(),"上海"); session2.doWork(new Work(){ @Override public void execute(Connection connection) throws SQLException { System.out.println("getCurrentSession2 connection hashCode" + connection.hashCode()); } }); session2.save(s); transaction.commit();
这里是结果:
getCurrentSession1 connection hashCode32089354
Hibernate:
insert
into
hibernate.STUDENT
(SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
values
(?, ?, ?, ?, ?)
getCurrentSession2 connection hashCode22794654
Hibernate:
insert
into
hibernate.STUDENT
(SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
values
(?, ?, ?, ?, ?)
getCurrentSession1 connection hashCode1648001170 Hibernate: insert into STUDENTS (NAME, GENDER, BIRTHDAY, ADDRESS, SID) values (?, ?, ?, ?, ?) getCurrentSession2 connection hashCode1648001170 Hibernate: insert into STUDENTS (NAME, GENDER, BIRTHDAY, ADDRESS, SID) values (?, ?, ?, ?, ?)
我试了一下你的代码,上面是运行结果,是一样的
你设置property那里了吗?
很惊奇,没错啊,我想看一下项目文件
Hibernate初探之单表映射
74810 学习 · 793 问题
相似问题