2015-04-14 60 views
1

我是新的Hibernate配置,我試圖設置一個這個測試應用程序,但我收到一些問題。我做了一切,因爲在本教程中: http://www.laliluna.de/articles/java-persistence-hibernate/first-hibernate-example-tutorial.html但似乎我我做錯了什麼:問題配置休眠和PostgresSql

查看日誌:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
SLF4J: Defaulting to no-operation (NOP) logger implementation 
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 
Apr 14, 2015 10:27:28 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} 
Apr 14, 2015 10:27:29 AM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.3.8.Final} 
Apr 14, 2015 10:27:29 AM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5} 
Apr 14, 2015 10:27:29 AM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Apr 14, 2015 10:27:30 AM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Apr 14, 2015 10:27:30 AM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
Apr 14, 2015 10:27:33 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity 
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at de.laliluna.example.TestExample.createHoney(TestExample.java:92) 
    at de.laliluna.example.TestExample.main(TestExample.java:28) 
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2165) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:202) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:47) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2077) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:184) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:47) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2056) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:178) 
    at de.laliluna.hibernate.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:32) 
    ... 2 more 
Caused by: org.dom4j.DocumentException: Error on line 14 of document : The element type "session-factory" must be terminated by the matching end-tag "</session-factory>". Nested exception: The element type "session-factory" must be terminated by the matching end-tag "</session-factory>". 
    at org.dom4j.io.SAXReader.read(SAXReader.java:482) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157) 
    ... 10 more 

我得到了以下配置:

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<!-- Generated by MyEclipse Hibernate Tools.     --> 
<hibernate-configuration> 

    <session-factory> 
    <!-- PostgreSQL connection --> 
    <property name="connection.url">jdbc:postgresql://localhost:5432/firsthibernate</property> 
    <property name="connection.username">postgres</property> 
    <property name="connection.password">root</property> 
    <property name="connection.driver_class">org.postgresql.Driver</property> 
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 


    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 
    <!-- thread is the short name for 
     org.hibernate.context.ThreadLocalSessionContext 
     and let Hibernate bind the session automatically to the thread 
    --> 
    <property name="current_session_context_class">thread</property> 
    <!-- this will show us all sql statements --> 
    <property name="hibernate.show_sql">true</property> 
    <!-- this will create the database tables for us --> 
    <property name="hibernate.hbm2ddl.auto">create</property> 
    <!--<mapping resource="de/laliluna/example/Honey.hbm.xml" />--> 
    <mapping class="de.laliluna.example.Honey" /> 


    </session-factory> 

</hibernate-configuration> 

類mappi NG:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 
<hibernate-mapping package="de.laliluna.example"> 
    <class name="Honey" table="thoney"> 
     <id name="id"> 
      <!-- PostgreSQL generator for a sequence which is named honey_id_seq 
      --> 
      <generator class="sequence"> 
       <param name="sequence">honey_id_seq</param> 
      </generator> 

      <!-- MySQL generator for a increment field 
      <generator class="increment"/> 
      --> 
     </id> 
     <property name="name" column="fooname"/> 
     <property name="taste" column="bartaste"/> 

    </class> 

</hibernate-mapping> 
+0

檢查您的應用程序服務器是否有相同的文件,並在日誌中警告您使用http://www.hibernate.org/dtd/而不是http://hibernate.sourceforge.net/hibernate-configuration- 3.0.dtd我檢查你的xml,它是有效的。 – erhun

+0

@erhun我檢查並發現這是問題,它似乎不是一個簡單的警告。 警告:HHH000223:識別過時的hibernate命名空間http://hibernate.sourceforge.net/。改爲使用命名空間http://www.hibernate.org/dtd/。 參考Hibernate 3.6遷移指南! – Eduardox23

+0

@ Edardox23你可以接受我的答案,因爲將來人們可以很容易地找到解決這個問題的方法。 – erhun

回答

0

檢查你的應用程序服務器具有相同的文件,並在日誌中它提醒你作爲使用

http://www.hibernate.org/dtd/

,而不是

http://hibernate.sourceforge.net/

我檢查你的xml,它是有效的。

WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at de.laliluna.example.TestExample.createHoney(TestExample.java:92)