Hi,
I use Hibernate 2.1.6 and use versioning for concurrency and things work just fine. I have a problem with a side-effect after "reordering" a Collection List. Reordering means that two of the items switch place with each other.
I let the user move Items up and down in the GUI and behind the scene I use the .set method on my (Hibernate managed) List and it seems to work just fine but when I later want to update one of the moved child items i get a StaleObjectStateException. If I simply retry the same thing again the versioning seems to get back in sync and I can update the moved object.
Here is a snip (parts) from my mapping (XDoclet generated):
Code:
<timestamp
name="lastUpdatedDatetime"
column="LAST_UPDATE"
/>
<list
name="appLayers"
table="APP_LAYER"
lazy="false"
inverse="false"
cascade="all"
>
<key
column="ID_APP"
>
</key>
<index
column="LAYER_ORDER_NO"
/>
<one-to-many
class="se.xxxxxxxx.AppLayer"
/>
</list>
Here is my reordering code:
Code:
Transaction tx = null;
try
{
Session session = HibernateSession.getSession();
tx = session.beginTransaction();
Application app = (Application)session.load(Application.class,applicationid);
AppLayer theLayer = app.getApplayerById(applayerid);
List appLayers = app.getAppLayers();
int currentPosition = appLayers.indexOf(theLayer);
int newposition = currentPosition+positionAdjuster; // -1 or +1
// Check newposition validity!
if ((newposition > (appLayers.size()-1))||(newposition < 0))
{
c_Log.debug("Skipping layermove as new position is out of possible scope. newpos="+newposition+" layerList size="+appLayers.size());
tx.commit();
return;
}
AppLayer theSwapLayer = (AppLayer)appLayers.get(newposition);
appLayers.set(newposition,theLayer);
appLayers.set(currentPosition,theSwapLayer);
// Trigger save of new collection ordering....
session.update(app);
session.flush();
tx.commit();
catch (HibernateException he)
{
try
{
if (tx != null) tx.rollback();
}
catch (HibernateException heRoll)
{
c_Log.fatal("Could not rollback!",heRoll);
}
c_Log.fatal("Could not move layer (id="+applayerid+") "+direction+"wards.",he);
throw new ServiceException(he,ServiceException.SRV_EXC_NORMAL);
}