码迷,mamicode.com
首页 > Web开发 > 详细

hibernate配置

时间:2015-01-30 17:16:07      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

今天在学习Hibernate、在配置的过程报了一个如下的错误、

  org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set

仔细检查了一下、也没发现有什么错、配置也都是参照官方文档写的、

后来在网上查了一下、说是Hibernate  4.X之后废弃了原来的buildSessionFactory方法、改为最新的后、就可以正常运行了、代码如下、

错误的代码:

                Familly f = new Familly();
		f.setFamilyid(1);
		f.setSex("1");
		f.setCifname("杨");
		f.setRelname("军");
		
		Configuration cfg = new Configuration();
		SessionFactory sessionFactory = cfg.configure().buildSessionFactory(new StandardServiceRegistryBuilder().build());
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(f);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();

  

修改后的正确代码:

                Familly f = new Familly();
		f.setFamilyid(1);
		f.setSex("1");
		f.setCifname("杨");
		f.setRelname("军");
		
		Configuration cfg = new Configuration().configure();
		StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
		                    .applySettings(cfg.getProperties()).build();
		SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
		
		/* 废弃一下
		Configuration cfg = new Configuration();
		SessionFactory sessionFactory = cfg.configure().buildSessionFactory(new StandardServiceRegistryBuilder().build());
		*/
		
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(f);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();

打印生成的SQL如下:

Hibernate: insert into CR_FAMILY (cifname, relname, sex, familyid) values (?, ?, ?, ?)

hibernate配置

标签:

原文地址:http://www.cnblogs.com/keryang/p/4262408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!