Hibernate4后出现: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect'
2016-10-18 03:10
441 查看
之前的代码:
package com.entity;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class TestStudents {
@Test
public void testschemaExport(){
//创建配置对象:
Configuration config = new Configuration();
//创建服务对象:
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//生成sessionfactory:
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true,true);
}
}
注意第注释://创建配置对象: 这个注释后的代码;
Configuration config = new Configuration();
applySettings(cfg.getProperties())是获取hibernate.cfg.xml的配置信息后设置进ServiceRegistryBuilder中,但是在设置之前的cfg(也就是Configuration)没有获取到hibernate.cfg.xml中的信息,所以会出现在运行的时候或出现属性值未设置的情况。
所以现在就好办了只要在applySettings(cfg.getProperties())之前获取hibernate.cfg.xml的信息并设置到ServiceRegistryBuilder中就行,这样ServiceRegistryBuilder就可以通过这些信息来构建ServiceRegistry。可以通过Configuration的configure()方法获取的hibernate.cfg.xml中的配置信息。
修改后的代码:
package com.entity;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class TestStudents {
@Test
public void testschemaExport(){
//创建配置对象:
Configuration config = new Configuration().configure();
//创建服务对象:
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//生成sessionfactory:
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true,true);
}
}
这样就对了
哈哈! 附上成果图!
package com.entity;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class TestStudents {
@Test
public void testschemaExport(){
//创建配置对象:
Configuration config = new Configuration();
//创建服务对象:
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//生成sessionfactory:
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true,true);
}
}
注意第注释://创建配置对象: 这个注释后的代码;
Configuration config = new Configuration();
applySettings(cfg.getProperties())是获取hibernate.cfg.xml的配置信息后设置进ServiceRegistryBuilder中,但是在设置之前的cfg(也就是Configuration)没有获取到hibernate.cfg.xml中的信息,所以会出现在运行的时候或出现属性值未设置的情况。
所以现在就好办了只要在applySettings(cfg.getProperties())之前获取hibernate.cfg.xml的信息并设置到ServiceRegistryBuilder中就行,这样ServiceRegistryBuilder就可以通过这些信息来构建ServiceRegistry。可以通过Configuration的configure()方法获取的hibernate.cfg.xml中的配置信息。
修改后的代码:
package com.entity;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class TestStudents {
@Test
public void testschemaExport(){
//创建配置对象:
Configuration config = new Configuration().configure();
//创建服务对象:
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//生成sessionfactory:
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true,true);
}
}
这样就对了
哈哈! 附上成果图!
相关文章推荐
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set .
- hibernate第二篇 org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' no
- Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set 解决方法
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dia
- Exception in thread "main" org.hibernate.HibernateException: Connection cannot be null when 'hiberna
- failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hiber
- 异常记录001—hibernate 4.2.4 Final 测试出现Connection cannot be null when 'hibernate.dialect' not set
- Connection cannot be null when 'hibernate.dialect' not set
- Hibernate Connection cannot be null when 'hibernate.dialect' not set错误
- ssh整合Caused by: org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection
- org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
- ExceptionInInitialize Access to DialectResolutionInfo cannot be null when 'hibernate.dialect'not set
- 马士兵的第一个hibernate程序(Connection cannot be null when 'hibernate.dialect' not set)
- HIbernate4.2.7错误Connection cannot be null when 'hibernate.dialect' not set
- Connection cannot be null when 'hibernate.dialect' not set问题的解决
- Connection cannot be null when 'hibernate.dialect' not set
- 异常:org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available