hibernate配置
今天在学习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 (?, ?, ?, ?)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。