慕瓜3482629
2016-11-27 20:20
为什么我执行测试getcurrentsession保存学生对象的时候,hashcode不一样,执行的也是这个类,看见有同学也遇到一样的问题,希望大神针对这个问题给个合理的答案。
@Test
public void testsaveStudentsWithgetCurrentSession(){
Configuration config=new Configuration().configure();
//获得服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionfactory对象那个
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
Session session1=sessionFactory.getCurrentSession();
Transaction transaction=session1.beginTransaction();
//生成一个学生对象
Students s=new Students(1,"张三","男",new Date(),"北京");
session1.doWork(new Work(){
@Override
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.println("connection.hashCode:"+connection.hashCode());
}
});
session1.save(s);
transaction.commit();
Session session2=sessionFactory.getCurrentSession();
transaction=session2.beginTransaction();
s=new Students(2,"李四","男",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);
transaction.commit();
}
}
在配置文档中配置过了,为什么hashcode不一样呢
你好,我试了一你的下代码是没有问题的,hashcode是相同的。你看看是不是运行的不是这个方法,还是看错地方了
我做出来了,跟视频一样,你需要源码吗?
还是不一样,测试这个结果,主要是这个测试代码,跟其他的配置什么的没有关系吧。
@Test
public void testSaveStudentsWithGetCurrentSession()
{
Configuration config=new Configuration().configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
Session session=sessionFactory.getCurrentSession();
Transaction transaction=session.beginTransaction();
Students s=new Students(1,"长官","男",new Date(),"武当");
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.print("我的hashCode是:"+connection.hashCode());
}
});
session.save(s);
transaction.commit();
Session session1=sessionFactory.getCurrentSession();
transaction=session1.beginTransaction();
s=new Students(2,"长官1","男",new Date(),"武当");
session1.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.print("我的hashCode是:"+connection.hashCode());
}
});
session1.save(s);
transaction.commit();
}
Hibernate初探之单表映射
74810 学习 · 793 问题
相似问题