标签:
今天在学习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 (?, ?, ?, ?)
标签:
原文地址:http://www.cnblogs.com/keryang/p/4262408.html