I'm putting together an application that uses composite keys for each of the objects. The objects are versioned so basically all the keys look like:
( long seq, int version )
To do this I followed the steps recommended elsewhere and use an id declaration of:
Code:
<composite-id name="primaryKey" unsaved-value="any">
<key-property name="id" column="id"/>
<key-property name="version" column="version"/>
</composite-id>
and am setting using a SequenceGenerator to generate my key prior to calling session.save ().
The problem, of course, is that Hibernate has no clue when an object is saved or needs saving/updating.
How can I get it to behave properly in sess.save of a linked object?
I've come up with the following solution that seems to work but it seems quite inelegant. Is this the right or only way?
1) I created an implementation of Interceptor and add it to each session that i use
2) I updated the primary key object that I'm using in my classes to include a 'saved' bit that I can tweak:
Code:
public class FooKVO {
public long id; // persisted
public int version; // persisted
public boolean saved; // transient
}
3) in Interceptor.onLoad and Interceptor.onSave i set that bit for applicable objects using:
Code:
if (o intsanceof Foo) {
final Foo foo = (Foo) o;
foo.getPrimaryKey ().setSaved (true);
}
return;
4) I implemented Interceptor.isUnsaved as:
Code:
if (o intsanceof Foo) {
final Foo foo = (Foo) o;
if (foo.getPrimaryKey ().getSaved ()) {
return Boolean.FALSE;
}
}
return;
Any thoughts or guidance on this?