Hi there ,
I use the following mapping for a composite id
Mapping documents:
Code:
<composite-id unsaved-value="any">
<key-property name="begDate" column="BEG_DATE" type="java.util.Date" length="7"/>
<key-property name="id" column="ID" type="java.lang.Integer" length="8"/>
<key-property name="recordId" column="RECORD_ID" type="com.xxx.usertypes.PrimaryKeyIdType"/>
</composite-id>
As you can see I use a custom UserType for one of the key properties
type="com.xxx.usertypes.PrimaryKeyIdType"This usertype calls an oracle sequence to generate a new value for that property and this works as expected and this is the code for the nullSafeSet()
Code:
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
String sql = "SELECT seq_the_seq.nextval from dual ";
Statement s = st.getConnection().createStatement();
ResultSet rs = s.executeQuery(sql);
if (value == null) {
if (rs.next()) {
int nextVal = rs.getInt(1);
value = new Integer(nextVal);
st.setInt(index, nextVal);
rs.close();
} else {
log.error("ResultSet was empty, after trying to generate next ID value ");
}
} else {
st.setInt(index, ((Integer) value).intValue());
}
}
So Session.save(Object o) creates a new row as expected.
My problem is now, that after saving the object, it's value for that one property
recordId is still null.
What do I need to do to ensure that the key-property will be set with the value created by the sequence ?
Is the UserType the right approach for that our should I go with CompositeUserType ?
I know that the DB-design may look strange but that's the way I have to live with.
Thanks in advance,
Dirk