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的源码中找到了 

[java] view plaincopy
  1. final ServiceRegistry serviceRegistry =  new ServiceRegistryBuilder()  
  2.     .applySettings( properties )  
  3.     .buildServiceRegistry();  


 

就写了个

[java] view plaincopy
  1. Configuration().configure().buildSessionFactory(new ServiceRegistryBuilder() .buildServiceRegistry())  


但是报错了

[java] view plaincopy
  1. WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections  
  2. Initial SessionFactory creation failed.org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set  
  3. Exception in thread "main" java.lang.ExceptionInInitializerError  
  4.     at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)  
  5.     at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:9)  
  6.     at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:22)  
  7.     at org.hibernate.tutorial.EventManager.main(EventManager.java:14)  
  8. Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set  
  9.     at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)  
  10.     at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)  
  11.     at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)  
  12.     at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)  
  13.     at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)  
  14.     at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)  
  15.     at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)  
  16.     at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)  
  17.     at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)  
  18.     at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)  
  19.     at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)  
  20.     ... 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
  1. Configuration cfg=new Configuration().configure();  
  2.     Properties properties=cfg.getProperties();  
  3.     Environment.verifyProperties(properties);  
  4.     ConfigurationHelper.resolvePlaceHolders( properties );  
  5.           return cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry());  

这样就不会报那个错了 但是这些代码都基本上源自c.buildSessionFactory()中的


6:

 

[html] view plaincopy
  1. ERROR: HHH000197: Error parsing XML: /hibernate.cfg.xml(12) The reference to entity "characterEncoding" must end with the ';' delimiter.  
  2. Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml  
  3. Exception in thread "main" java.lang.ExceptionInInitializerError  
  4.     at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)  
  5.     at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:8)  
  6.     at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:23)  
  7.     at org.hibernate.tutorial.EventManager.main(EventManager.java:16)  
  8. Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml  
  9.     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2013)  
  10.     at org.hibernate.cfg.Configuration.configure(Configuration.java:1925)  
  11.     at org.hibernate.cfg.Configuration.configure(Configuration.java:1904)  
  12.     at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)  
  13.     ... 3 more  
  14. 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.  
  15.     at org.dom4j.io.SAXReader.read(SAXReader.java:482)  
  16.     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2006)  
  17.     ... 6 more  


原因 

[java] view plaincopy
  1. <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>  

应该写成

[java] view plaincopy
  1. <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>  

0 0
原创粉丝点击