简体   繁体   中英

Problems to configure hibernate and PostgreSQL

I am new to Hibernate configuration, and I'm trying to setup a this test application but I am getting a few problems. I did everything as in this tutorial: http://www.laliluna.de/articles/java-persistence-hibernate/first-hibernate-example-tutorial.html But it seems that I am doing something wrong:

See the log:

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

I got the Following Configuration:

<?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>

Class mapping:

<?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>

Check you application server has the same file and in logs it warn you as use

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

instead of

http://hibernate.sourceforge.net/

I check your xml and it is valid.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM