2

I am new to hibernte. I was trying a simple retrieval of records from oracle table using hibernate.

My entity class is named pojo1 and pojo1.hbm.xml is as follows:

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

I am getting the following error while running the app class containing all the transaction code:

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

Kindly help.

Ani
  • 123
  • 1
  • 1
  • 14

2 Answers2

3

You have to declare an id for your entity class using <id> tag.

So change this:

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

to this:

<id name="id" column="TRANSLATION_ID" type="int"></id>
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
2

You need an <id> or <composite-id> element inside your <class> element.

See the documentation here.

Mapped classes must declare the primary key column of the database table.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • But id is equivalent to primary key of table right? What if I dont have/need one? @Robby – Ani Aug 26 '14 at 05:19
  • Then it appears that you're SOL. See also [here](http://stackoverflow.com/questions/767277/hibernate-and-no-pk). Suggestions to get around it [here](http://stackoverflow.com/questions/15829848/to-map-a-database-view-with-no-primary-key-in-hibernate-xml-mapping). – Robby Cornelissen Aug 26 '14 at 05:23