我是Hibernate配置的新手,我正在尝试设置这个测试应用程序,但是我遇到了一些问题。我在本教程中做了所有事情: http://www.laliluna.de/articles/java-persistence-hibernate/first-hibernate-example-tutorial.html但似乎我做错了什么:
见日志:
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>
类映射:
<?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 :(得分:0)
检查您的应用程序服务器是否具有相同的文件,并在日志中警告您使用
而不是
我检查你的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)