hibernate初学者可能碰到的一些问题
2014-08-31 19:20
330 查看
1:方言(dialect)写错了 导致出现
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown table 'system_sequences' in information_schema 修改成正确的dialect就OK了
2:找不到实体bean
Exception in thread "main" org.hibernate.MappingException: Unknown entity:org.hibernate.tutorial.domain.Event
未配置resource 加上<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>就OK了
3:没有加DTD导致无法读取hbm.xml文件
Error parsing XML (2) : cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML
在hbm.xml文件中加上
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">就OK了
4:Field 'EVENT_ID' doesn't have a default value
由于我的generator为native 所以修改方法是重新创建表 CREATE TABLE EVENTS( EVENT_ID INT AUTO_INCREMENT PRIMARY KEY ,
title VARCHAR(50),
EVENT_DATE DATE); (我用的是mySql 对于主键生成器暂时没仔细研究 所以只好这样改了)
5:由于hibernate4.0 不建议使用Configuration().configure().buildSessionFactory()而建议使用Configuration().configure().buildSessionFactory(ServiceRegistry)
第一次使用不知道怎么使用 查看api胡乱的写了几个ServiceRegistry的实现 都有问题 最后去Configure的源码中找到了
[java] view
plaincopy
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings( properties )
.buildServiceRegistry();
就写了个
[java] view
plaincopy
Configuration().configure().buildSessionFactory(new ServiceRegistryBuilder() .buildServiceRegistry())
但是报错了
[java] view
plaincopy
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Initial SessionFactory creation failed.org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:9)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:22)
at org.hibernate.tutorial.EventManager.main(EventManager.java:14)
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 3 more
在http://www.coderanch.com/t/564214/ORM/java/Hibernate-configuration-connection-provider-or找到相似的问题
这位哥们的解决方法是不要用新的方法用原来的那个旧方法
截取至其中的一段回复
The problem is c.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry());
c.buildSessionFactory() is the way to go, even though it has been deprecated in hibernate 4.0.0.
Looking into what it does, it creates a serviceregistry and then copies the properties from the configuration over into the serviceregistry. It is not enough to use an empty service registry. It appears that the intent was to deprecate the entire Configuration
class in hibernate 4.0.0. I have not found a hibernate configuration example that only uses the service registry and does not use configuration yet.
I chalk this up to the unhelpful deprecation documentation on c.buildSessionFactory().
根据源代码 写了个没有意义的代码 只是纯粹为了解决这个问题 如何哪位高手知道 请一定要留言告诉我
[java] view
plaincopy
Configuration cfg=new Configuration().configure();
Properties properties=cfg.getProperties();
Environment.verifyProperties(properties);
ConfigurationHelper.resolvePlaceHolders( properties );
return cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry());
这样就不会报那个错了 但是这些代码都基本上源自c.buildSessionFactory()中的
6:
[html] view
plaincopy
ERROR: HHH000197: Error parsing XML: /hibernate.cfg.xml(12) The reference to entity "characterEncoding" must end with the ';' delimiter.
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:23)
at org.hibernate.tutorial.EventManager.main(EventManager.java:16)
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2013)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1925)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1904)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 3 more
Caused by: org.dom4j.DocumentException: Error on line 12 of document : The reference to entity "characterEncoding" must end with the ';' delimiter. Nested exception: The reference to entity "characterEncoding" must end with the ';' delimiter.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2006)
... 6 more
原因
[java] view
plaincopy
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
应该写成
[java] view
plaincopy
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown table 'system_sequences' in information_schema 修改成正确的dialect就OK了
2:找不到实体bean
Exception in thread "main" org.hibernate.MappingException: Unknown entity:org.hibernate.tutorial.domain.Event
未配置resource 加上<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>就OK了
3:没有加DTD导致无法读取hbm.xml文件
Error parsing XML (2) : cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML
在hbm.xml文件中加上
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">就OK了
4:Field 'EVENT_ID' doesn't have a default value
由于我的generator为native 所以修改方法是重新创建表 CREATE TABLE EVENTS( EVENT_ID INT AUTO_INCREMENT PRIMARY KEY ,
title VARCHAR(50),
EVENT_DATE DATE); (我用的是mySql 对于主键生成器暂时没仔细研究 所以只好这样改了)
5:由于hibernate4.0 不建议使用Configuration().configure().buildSessionFactory()而建议使用Configuration().configure().buildSessionFactory(ServiceRegistry)
第一次使用不知道怎么使用 查看api胡乱的写了几个ServiceRegistry的实现 都有问题 最后去Configure的源码中找到了
[java] view
plaincopy
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings( properties )
.buildServiceRegistry();
就写了个
[java] view
plaincopy
Configuration().configure().buildSessionFactory(new ServiceRegistryBuilder() .buildServiceRegistry())
但是报错了
[java] view
plaincopy
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Initial SessionFactory creation failed.org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:9)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:22)
at org.hibernate.tutorial.EventManager.main(EventManager.java:14)
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 3 more
在http://www.coderanch.com/t/564214/ORM/java/Hibernate-configuration-connection-provider-or找到相似的问题
这位哥们的解决方法是不要用新的方法用原来的那个旧方法
截取至其中的一段回复
The problem is c.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry());
c.buildSessionFactory() is the way to go, even though it has been deprecated in hibernate 4.0.0.
Looking into what it does, it creates a serviceregistry and then copies the properties from the configuration over into the serviceregistry. It is not enough to use an empty service registry. It appears that the intent was to deprecate the entire Configuration
class in hibernate 4.0.0. I have not found a hibernate configuration example that only uses the service registry and does not use configuration yet.
I chalk this up to the unhelpful deprecation documentation on c.buildSessionFactory().
根据源代码 写了个没有意义的代码 只是纯粹为了解决这个问题 如何哪位高手知道 请一定要留言告诉我
[java] view
plaincopy
Configuration cfg=new Configuration().configure();
Properties properties=cfg.getProperties();
Environment.verifyProperties(properties);
ConfigurationHelper.resolvePlaceHolders( properties );
return cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry());
这样就不会报那个错了 但是这些代码都基本上源自c.buildSessionFactory()中的
6:
[html] view
plaincopy
ERROR: HHH000197: Error parsing XML: /hibernate.cfg.xml(12) The reference to entity "characterEncoding" must end with the ';' delimiter.
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:23)
at org.hibernate.tutorial.EventManager.main(EventManager.java:16)
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2013)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1925)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1904)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 3 more
Caused by: org.dom4j.DocumentException: Error on line 12 of document : The reference to entity "characterEncoding" must end with the ';' delimiter. Nested exception: The reference to entity "characterEncoding" must end with the ';' delimiter.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2006)
... 6 more
原因
[java] view
plaincopy
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
应该写成
[java] view
plaincopy
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
相关文章推荐
- 关于无法正常浏览web服务器可能碰到的一些问题
- Torch代码书写时可能碰到的一些问题
- hibernate初学者可能碰到的一些问题
- 关于一些新手MFC入门时,在vs上运行孙鑫老师代码时可能碰到的问题。
- android studio 将已有的项目 以module Library的 形式引入到 现在的项目中可能碰到的一些问题
- windows moblie开发中读取XML文件所碰到的一些问题
- 使用Ajax技术可能面临的一些问题
- JfreeChart使用经验总结(分析了使用中碰到的一些常见问题)
- 开发中碰到过的一些问题总结,给自己也给大家一个参考!
- Solaris 10 安装及SVC管理及X及Vmware及其它可能遇到的一些问题
- 目前我碰到的PCAnyWhere 的几个问题(可能是BUG)
- fedora6的安装及一些可能遇到问题的解决方法
- 初学java碰到的一些问题
- 隐藏任务栏最大化程序窗口中碰到的一些问题及应对方法
- .Net在写托盘程序中碰到的一些问题的解决方法(无法关机/自启动等)
- 对于泛型的自动匹配的一个问题,可能你也会碰到的
- 电脑开机显示您可能是软件盗版的受害者!删除的一些问题
- 前几天在搞rss的解析,碰到一些问题
- 写PL/SQL程序时碰到的一些问题
- Solaris 10 安装及SVC管理及X及Vmware及其它可能遇到的一些问题