Hi
I'm having a bit of an issue with version. When the value of the version matches the value of my version's unsaved-value, hibernate is issuing an SQL UPDATE instead of an SQL INSERT.
I am trying to do it this way as I would like the ID to be application defined, rather than having a hibernate-generated ID.
I've done a fair bit of documentation reading, googling and forum reading on this matter but none of the suggestions have worked so far. Basically whenever I try a session.saveOrUpdate, hibernate will try an update, rather than an insert, even though my version is set to null (the unsaved-value).
Hibernate version: 2.1.8
Database: Oracle 10g
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- Agency Details -->
<class name="com.thistle.portlet.exts.trust.Agency" table="Agency" dynamic-update="true" optimistic-lock="version">
<id name="iata" column="iata" type="java.lang.String" unsaved-value="any"/>
<version
column="sys_version"
name="_version"
type="integer"
unsaved-value="null"
/>
<property name="name">
<column name="name"/>
</property>
<set name="trusted" cascade="all" lazy="false" table="Agency_Trusted_Corporation">
<key column="agency_iata"/>
<many-to-many column="corp_id"
lazy="false"
unique="false"
class="com.thistle.portlet.exts.trust.Corporation"/>
</set>
<set name="reserved" cascade="all" lazy="false" table="Agency_Reserved_Corporation">
<key column="agency_iata"/>
<many-to-many column="corp_id"
lazy="false"
unique="false"
class="com.thistle.portlet.exts.trust.Corporation"/>
</set>
<set name="reservedTravellers" cascade="all" lazy="false" table="Agency_Reserved_Traveller">
<key column="agency_iata"/>
<many-to-many column="trav_opera_id"
lazy="false"
unique="false"
class="com.thistle.portlet.exts.trust.Traveller"/>
</set>
</class>
<!-- Corporate details as -->
<class name="com.thistle.portlet.exts.trust.Corporation" table="Corporation" dynamic-update="true" optimistic-lock="version">
<id name="cid" column="cid" type="java.lang.String" unsaved-value="any"/>
<version
column="sys_version"
name="_version"
type="integer"
unsaved-value="null"
/>
<property name="name">
<column name="name" />
</property>
<set name="reservedTravellers" cascade="all" lazy="false" table="Corporate_Reserved_Traveller">
<key column="corp_id"/>
<many-to-many column="trav_opera_id"
lazy="false"
unique="false"
class="com.thistle.portlet.exts.trust.Traveller"/>
</set>
</class>
<!-- Traveller details as -->
<class name="com.thistle.portlet.exts.trust.Traveller" table="Traveller" dynamic-update="true" optimistic-lock="version">
<id name="operaId" column="operaId" type="java.lang.String" unsaved-value="any"/>
<version
column="sys_version"
name="_version"
type="integer"
unsaved-value="null"
/>
</class>
</hibernate-mapping>
All my hibernate objects extend HibernateItem, and as such have their _version Integer set to null, if it is a new item:
Code:
public class HibernateItem {
private Integer _version = null;
...
getter and setter...
...
}
saveOrUpdate method:Code:
public void saveOrUpdateAgency(final Agency agency) {
log.debug("Attempting to commit Agency.");
log.debug("IATA: "+agency.getIata());
log.debug("VERSION: "+agency.get_version());
log.debug("NAME: "+agency.getName());
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(final Session session) throws HibernateException {
session.save(agency);
return null;
}
};
}
Log:The logs show the version going in as null.
Code:
Attempting to commit Agency.
IATA: iata_test0
VERSION: null
NAME: Test Agency 0
However, despite this fact, hibernate does not do an insert, instead it runs an update, which naturally updates nothing, as there is nothing in the tables with the same IATA, or CID (which are my IDs).
Any help much appreciated! Thanks