当我尝试启动我的hibernate应用程序时,我得到一个InvalidMappingException,但我找不到映射文件语法中的任何错误。补充说明:
映射文件:
<?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="media.definitions">
<class name="Category" table="GET_CATEGORY" mutable="false">
<id name="id" column="ID" type="integer">
<generator class="identity" />
</id>
<property name="name" column="BEZEICHNUNG" type="string" />
<set name="children" inverse="true" lazy="true">
<key column="OBERKATEGORIE" type="integer" />
<index column="INDEX"/>
<one-to-many class="Category" />
</set>
<many-to-one name="parent" column="OBERKATEGORIE" class="Category"
not-null="false" cascade="all" />
</class>
</hibernate-mapping>
错误:
[java] log4j:WARN Please initialize the log4j system properly.
[java] Initial SessionFactory creation failed: org.hibernate.InvalidMapping
Exception: Could not parse mapping document from resource media/definitions/Cate
gory.hbm.xml
[java] org.hibernate.InvalidMappingException: Could not parse mapping docum
ent from resource media/definitions/Category.hbm.xml
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.jav
a:569)
[java] at media.mediadbimpl.MediaDbImpl.init(MediaDbImpl.java:103)
[java] at media.frontend.Media.<init>(Media.java:50)
[java] at media.frontend.Media.main(Media.java:156)
[java] Caused by: org.hibernate.InvalidMappingException: Could not parse ma
pping document from invalid mapping
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.
java:502)
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.jav
a:566)
[java] ... 3 more
[java] Caused by: org.xml.sax.SAXParseException; lineNumber: 14; columnNumb
er: 49; Attribut "type" muss f³r Elementtyp "key" deklariert werden.
[java] at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.c
reateSAXParseException(ErrorHandlerWrapper.java:203)
[java] at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.e
rror(ErrorHandlerWrapper.java:134)
[java] at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.repo
rtError(XMLErrorReporter.java:437)
[java] at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.repo
rtError(XMLErrorReporter.java:368)
[java] at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.repo
rtError(XMLErrorReporter.java:325)
[java] at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.a
ddDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1253)
[java] at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.h
andleStartElement(XMLDTDValidator.java:1917)
[java] at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.e
mptyElement(XMLDTDValidator.java:763)
[java] at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerI
mpl.scanStartElement(XMLNSDocumentScannerImpl.java:356)
[java] at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentSc
annerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
[java] at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImp
l.next(XMLDocumentScannerImpl.java:606)
[java] at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerI
mpl.next(XMLNSDocumentScannerImpl.java:117)
[java] at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentSc
annerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
[java] at com.sun.org.apache.xerces.internal.parsers.XML11Configuration
.parse(XML11Configuration.java:848)
[java] at com.sun.org.apache.xerces.internal.parsers.XML11Configuration
.parse(XML11Configuration.java:777)
[java] at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XM
LParser.java:141)
[java] at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.
parse(AbstractSAXParser.java:1213)
[java] at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAX
Parser.parse(SAXParserImpl.java:649)
[java] at org.dom4j.io.SAXReader.read(SAXReader.java:465)
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.
java:499)
[java] ... 4 more
[java] Java Result: 1
配置是硬编码的:
Configuration sessionConf = new Configuration()
.setProperty("hibernate.connection.driver_class", prop.getProperty("driver"))
.setProperty("hibernate.connection.url", prop.getProperty("url"))
.setProperty("hibernate.connection.username", prop.getProperty("user"))
.setProperty("hibernate.connection.password", prop.getProperty("pw"))
.setProperty("hibernate.connection.pool_size", "1")
.setProperty("hibernate.dialect", prop.getProperty("dialect"))
.setProperty("hibernate.hbm2ddl.auto", "update")
.setProperty("hibernate.current_session_context_class", "thread")
.setProperty("show_sql", "false")
.addResource("media/definitions/Category.hbm.xml");
sessionFactory = sessionConf.buildSessionFactory();
为什么我会收到此错误?
答案 0 :(得分:0)
根据hibernate映射DTD文件 - [hibernate-mapping-3.0.dtd][1]
,您的映射文件中有2个问题。
index
标记,因为您使用的集合类型为set
:DTD文件说:
声明集合索引的类型和列映射 (数组或列表索引,或地图的键)。
<index column="INDEX"/>
因此索引只能用于数组,列表和地图,它不能用于集合
type
标记内使用属性key
无效。根据DTD,密钥标记只能位于type
不属于列表的属性列表下方:
<!ELEMENT key (column*)>
<!ATTLIST key column CDATA #IMPLIED>
<!ATTLIST key property-ref CDATA #IMPLIED>
<!ATTLIST key foreign-key CDATA #IMPLIED>
<!ATTLIST key on-delete (cascade|noaction) "noaction">
<!ATTLIST key not-null (true|false) #IMPLIED>
<!ATTLIST key update (true|false) #IMPLIED>
<!ATTLIST key unique (true|false) #IMPLIED>
因此,要解决您的问题,set
的映射应如下所示:
<set name="children" inverse="true" lazy="true">
<key column="OBERKATEGORIE"/>
<one-to-many class="Category" />
</set>