Hi ,
I am using the Null Object pattern (
http://www.refactoring.com/catalog/introduceNullObject.html) in my model.
I have a superclass A , and a null object N which is a subclass of A.
I have a situation where A is mapped to TABLE_A , and the discriminator column is TYPE.
Because N is a null object , I would not want it to be persisted into TABLE_A. It is discovered during runtime and should not be in TABLE_A.
I have a case where both A and N objects during runtime would be arranged in a certain sequence . After a sequence is arranged , I would need to capture a snapshot of the sequence and persist it into another table , say TABLE_ANOTHER. I would only need to store the OID into TABLE_ANOTHER.
How can I persist the null object instances into TABLE_ANOTHER ?
I tried using the subclass per table mapping (where the null object discriminator value is an arbitrary number -2 in the column TYPE to distinguish it from "actual" objects. Actual objects TYPE can is currently any integer number). But this method fails if I force TABLE_ANOTHER.OID to maintain referential integrity with TABLE_A.OID when performing a save.
If I drop the referential integrity , I get net.sf.hibernate.WrongClassException when I retrieve the object from TABLE_ANOTHER because I think internally Hibernate attempts to perform a join with TABLE_A and grab the discriminator value to create the actual object.
Is there a better solution to my problem ? Below is my existing mapping file.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="A"
table="TABLE_A"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="oid"
column="OID"
type="int"
>
<generator class="sequence">
</generator>
</id>
<discriminator
column="TYPE"
type="int"
/>
<property
name="type"
type="int"
update="false"
insert="false"
column="TYPE"
/>
<subclass
name="N"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="-2"
>
</subclass>
</class>
</hibernate-mapping>
Thanks.
Kean