hbm.xml文件中的一些错误

时间:2014-08-26 05:13:17

标签: hibernate

我是hibernte的新手。我正在尝试使用hibernate从oracle表中简单地检索记录。

我的实体类名为pojo1,pojo1.hbm.xml名称如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="hibernate" default-lazy="true">

<class name="pojo1" table="QUOTE_MESSAGE_TRANSLATION_BKP">
<meta attribute="class-description">
</meta>
<property name="id" column="TRANSLATION_ID" type="java.lang.Integer"/>
<property name="commitCode" column="COMMIT_CODE" type="java.lang.Integer"/>
</class>
</hibernate-mapping>

运行包含所有事务代码的app类时出现以下错误:

... ...
INFO: HHH000221: Reading mappings from resource: pojo1.hbm.xml
Aug 26, 2014 10:32:28 AM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (3) : The content of element type "class" must     match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-    id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
Aug 26, 2014 10:32:28 AM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (3) : The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Unable to read XML
Exception in thread "main" java.lang.ExceptionInInitializerError
at hibernate.app.main(app.java:20)
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:375)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:304)
at org.hibernate.cfg.Configuration.add(Configuration.java:516)
at org.hibernate.cfg.Configuration.add(Configuration.java:512)
at org.hibernate.cfg.Configuration.add(Configuration.java:686)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:769)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
at hibernate.app.main(app.java:17)
Caused by: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 12; The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at .... ....

请帮助。

2 个答案:

答案 0 :(得分:3)

您必须使用id代码为您的实体类声明<id>

所以改变这个:

<property name="id" column="TRANSLATION_ID" type="java.lang.Integer"/>

到此:

<id name="id" column="TRANSLATION_ID" type="int"></id>

答案 1 :(得分:2)

您的<id>元素中需要<composite-id><class>元素。

请参阅文档here

  

映射类必须声明数据库的主键列   表