-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: how to map version field used for optimistic locking?
PostPosted: Sun Apr 16, 2006 7:08 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 9:00 pm
Posts: 37
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 9:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I have not tried your sample to verify your problem - first look it appears a little odd to me. In anycase, I don't usually sub-elements here so I map versions something like
Code:
<version name="version" column="VERSION" type="long"/>


You may have other attributes as welll - see the ref doc.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 12:25 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 9:00 pm
Posts: 37
You are right, David.

The form you presented works.

But the form I used still comply with the DTD, so is this a bug with Hibernate?

Furthermore, this mapping is actually generated from Hibernate Tool 3.1 beta 4. So I hope the tool is doing the right thing.

Please advise,

Chuck


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 2:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I have not had time to verify your problem as it appears the DTD verification is complaining but the DTD looks OK. On the surface yes it looks odd but my gut tells me there some detail missing. I would think there would be a test in the test suite for it. In anycase, it is worth creating a simple example and submitting it to JIRA. Thanks for you efforts.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.