HIbernate4.2.7错误Connection cannot be null when 'hibernate.dialect' not set
2013-11-20 15:33
597 查看
今天在学习Hibernate的时候遇到一个错误,记录下来,开始养成写博客的习惯,错误的原因很简单。
错误内容如下:
主要原因参照第一行
Connection cannot be null when 'hibernate.dialect' not set。说是hibernate的dialect没有设置,但是在hibernate.cfg,xml中我已经配置了。主要内容如下:
Teacher.hbm.xml的配置如下:
junit的测试代码如下:
因为看的视频是前几年的,所用的HIBERNATE的版本比较早,所以对于Hibernate4.2.7的配置变动不是很清楚,在查阅了资料后总结出了和视频中不同的地方,但是还是报错了。最后发现,是在因为在建立SessionFactory之前,我的Configration少读了一次hibernate.cfg.xml中的配置,不然就要去找hibernate.properties,但是我的项目中没有对应的文件,则发生错误(没有设置数据库方言)。
新的代码改为:
测试通过,没有错误。增加了一句话:cf.configure();
总结:
在自学hibernate的时候, 资料的版本不对让进度上受到了很大的限制,英文的文档又不怎么看的懂。所以,要积攒之前的错误,尽量不要重复犯。也希望能找到人可以帮助指点一下。
错误内容如下:
org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:98) at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:68) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1822) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1780) at cn.yyx.hibernate.model.TeacherTest.test(TeacherTest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
主要原因参照第一行
Connection cannot be null when 'hibernate.dialect' not set。说是hibernate的dialect没有设置,但是在hibernate.cfg,xml中我已经配置了。主要内容如下:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property> <property name="connection.username">root</property> <property name="connection.password">222147</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping class="cn.yyx.hibernate.model.Teacher"/> </session-factory> </hibernate-configuration>
Teacher.hbm.xml的配置如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.yyx.hibernate.model.Person" table="Person"> <id name="id" column="teacher_id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="title"></property> </class> </hibernate-mapping>
junit的测试代码如下:
package cn.yyx.hibernate.model; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.Test; public class TeacherTest { @Test public void test() { Teacher t = new Teacher(); t.setName("xxx"); t.setTitle("high"); Configuration cf = new Configuration(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cf.getProperties()) .buildServiceRegistry(); SessionFactory sf = cf.buildSessionFactory(sr); Session s = sf.openSession(); s.beginTransaction(); s.save(t); s.getTransaction().commit(); s.close(); sf.close(); } }model类的代码如下:
package cn.yyx.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Teacher { private int id; private String name; private String title; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
因为看的视频是前几年的,所用的HIBERNATE的版本比较早,所以对于Hibernate4.2.7的配置变动不是很清楚,在查阅了资料后总结出了和视频中不同的地方,但是还是报错了。最后发现,是在因为在建立SessionFactory之前,我的Configration少读了一次hibernate.cfg.xml中的配置,不然就要去找hibernate.properties,但是我的项目中没有对应的文件,则发生错误(没有设置数据库方言)。
新的代码改为:
package cn.yyx.hibernate.model; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.Test; public class TeacherTest { @Test public void test() { Teacher t = new Teacher(); t.setName("xxx"); t.setTitle("high"); Configuration cf = new Configuration(); cf.configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cf.getProperties()) .buildServiceRegistry(); SessionFactory sf = cf.buildSessionFactory(sr); Session s = sf.openSession(); s.beginTransaction(); s.save(t); s.getTransaction().commit(); s.close(); sf.close(); } }
测试通过,没有错误。增加了一句话:cf.configure();
总结:
在自学hibernate的时候, 资料的版本不对让进度上受到了很大的限制,英文的文档又不怎么看的懂。所以,要积攒之前的错误,尽量不要重复犯。也希望能找到人可以帮助指点一下。
相关文章推荐
- Hibernate Connection cannot be null when 'hibernate.dialect' not set错误
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- 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
- hibernate第二篇 org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' no
- [Hibernate]Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set .
- [Hibernate]Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
- 马士兵的第一个hibernate程序(Connection cannot be null when 'hibernate.dialect' not set)
- Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set——【hibernate 日常错误】
- Connection cannot be null when 'hibernate.dialect' not set问题的解决
- Exception in thread "main" org.hibernate.HibernateException: Connection cannot be null when 'hiberna
- 创建SessionFactory出错Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
- ExceptionInInitialize Access to DialectResolutionInfo cannot be null when 'hibernate.dialect'not set
- 'hibernate.dialect' must be set when no Connection available
- Hibernate4后出现: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect'
- 'hibernate.dialect' must be set when no Connection avalable
- 'hibernate.dialect' must be set when no Connection available
- Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not