Hello,
This is my first attempt to add "version" field into table for use by optimistic locking with Hibernate 3.1. My mapping file is like this:
<hibernate-mapping>
<class name="Region" table="REGION">
<id name="regionid" type="long">
<column name="REGIONID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">seq_regionId</param>
</generator>
</id>
<version name="version" type="long">
<column name="VERSION" not-null="true" />
</version>
...
</class>
</hibernate-mapping>
When I try to run my simple test driver, I got following exception:
Caused by: org.xml.sax.SAXParseException: The content of element type "version" must match "(meta)*".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:398)
... 8 more
I checked the DTD
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd, which says:
<!ELEMENT version (meta*,column*)>
<!ATTLIST version name CDATA #REQUIRED>
....
So the mapping looks OK. All I can find in Hibernate docs also mapped similarly. Why SAXParseException is thrown?
If I change the version element to be a property element, the code runs successfully.
The Java class is like:
public class Region implements java.io.Serializable {
private long regionid;
private long version;
....
// getter/setters
}
My extremly simple test driver is like this:
public class StandaloneHib {
public static void main(String[] args) {
try {
// Create the SessionFactory from hibernate.cfg.xml
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Region r = new Region();
r.setStartdate(new Date());
//r.setVersion(System.currentTimeMillis());
r.setPrimarylocale(Locale.ENGLISH);
Serializable s = sessionFactory.openSession().save(r);
System.out.println("s = " + s.toString());
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("error = " + ex);
ex.printStackTrace();
}
}
}
I would like to know from anyone who has successfully mapped the version field how to do this properly.
Thx in advance,
Chuck