Connection cannot be null when 'hibernate.dialect' not set
程序员文章站
2022-04-13 14:09:14
...
学习hibernate的首日写了个小程序就出现了bug,找了点资料还是很快给解决了
刚开始的时候是:
package cn.edu.njupt.zzq.hibernatestudy.domain.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import cn.edu.njupt.zzq.hibernatestudy.domain.Student;
public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
Student s=new Student();
s.setId(2);
s.setName("强强");
s.setAge(20);
Configuration cgf=new Configuration();
/////////////////////////////////
SessionFactory sf=cgf.buildSessionFactory();
/////////////////////////////////////////
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
红色的地方出错了,应该写成,加上蓝色的方法
SessionFactory sf=cgf.configure().buildSessionFactory();
这样就行了啊!
但是上面这个方法已经过时了
下面给个不过时的:
public static void main(String[] args) {
Student s=new Student();
s.setId(3);
s.setName("强强");
s.setAge(20);
Configuration cgf=new Configuration();
//SessionFactory sf=cgf.configure().buildSessionFactory();
cgf.configure();//不要缺少或写在下面,这个是configure和serviceregistry之间
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cgf.getProperties()).buildServiceRegistry();
SessionFactory sf = cgf.buildSessionFactory(sr);
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
上一篇: hibernate注解联表查询以及联表导致的死循环
下一篇: springboot启动失败:Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
推荐阅读
-
ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法
-
Springboot启动出错:Access to DialectResolutionInfo cannot be null when hibernate.dialect not set
-
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
-
【Hibernate】Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set
-
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not Set
-
Connection cannot be null when 'hibernate.dialect' not set
-
springboot启动失败:Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
-
springboot启动出现Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
-
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 问题的解决方法
-
ExceptionInInitialize Access to DialectResolutionInfo cannot be null when 'hibernate.dialect'not set