hibernate初学者可能碰到的一些问题
来源:互联网 发布:apl美皇公司知乎 编辑:程序博客网 时间:2024/05/16 11:30
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的源码中找到了
- final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
- .applySettings( properties )
- .buildServiceRegistry();
就写了个
- Configuration().configure().buildSessionFactory(new ServiceRegistryBuilder() .buildServiceRegistry())
但是报错了
- 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().
根据源代码 写了个没有意义的代码 只是纯粹为了解决这个问题 如何哪位高手知道 请一定要留言告诉我
- 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:
- 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
原因
- <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
应该写成
- <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
- hibernate初学者可能碰到的一些问题
- hibernate初学者可能碰到的一些问题
- Torch代码书写时可能碰到的一些问题
- Hibernate使用过程碰到的一些问题小结
- 使用Hibernate Validator中碰到的一些问题
- eclipse maven插件可能碰到的问题
- ubuntu wireshark可能碰到的问题
- 使用genymotion模拟器可能碰到的问题
- 深交所 v5 行情 可能碰到的问题
- uc1698碰到的一些问题
- WCF碰到的一些问题
- mysql碰到的一些问题
- WCF碰到的一些问题
- 安卓适配碰到的一些问题
- 自己碰到的一些问题
- 最近碰到的一些问题
- WCF碰到的一些问题
- 做运维碰到的一些问题
- hdu1255 扫描线,矩形重叠面积(两次以上)
- 设计模式——三大工厂
- LeetCode之旅(33)
- 乌拉姆数列 Euler167
- 深搜dfs
- hibernate初学者可能碰到的一些问题
- iOS 博客资源精选
- OpenCV 实践程序12——用分类器对视频进行人脸检测
- Go_Json_Unmarshal_Marshal
- 付金额接姑婆过去
- 枚举和联合体
- hibernate 级联(cascade和inverse)
- 如何做文献综述:克雷斯威尔五步文献综述法
- $(function(){})与(function($){....})(jQuery)的区别