I have created an object hierarchy for auditing that has the class Audit as an abstract superclass. Subclasses extend Audit and one such subclass is JPProjectAudit. I am using a single table to hold all of the Audit records and use the name of the subclass as the discriminator.
The audit database table has a TARGET_OID column that is NOT NULL and holds the primary key to a few different types of objects depending on the Audit sub-class. For example, the JPProjectAudit subclass maps a JPProject's OID value in the TARGET_OID column, while the UserAccountAudit subclass maps a UserAccount OID value in the TARGET_OID column. There are no Foreign Key relationships in the database because of this multi-table relationship.
The mapping file is attached at the bottom of this message.
Bottom line is that when I attempt to persist a JPProjectAudit instance, in the same transaction as the JPProject, I get the following error message:
Code:
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: us.oh.state.dot.jpp.domain.JPProjectAudit.jpProject
Hibernate reports this problem because I have indicated 'unsaved-value="any"' on my JPProject id property because I am self-assigning id values. When Hibernate attempts to retrieve the 'id' property from the JPProject that my JPProjectAudit table has mapped, it goes through a series of checks, but unfortunately determines that the JPProject object is transient because 'any' id value is treated as such.
At this point, I think this may be an ordering bug when Hibernate organizes the entities in their persistable order. Since JPProject is reachable from JPProjectAudit, JPProject should be persisted first. That way, when Hibernate attempts to retrieve the ID value for the JPProject, the engine will not treat JPProject as a transient object. Either that, or there is a flaw in my mapping file or approach to the problem. Any ideas?
Code:
<?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 auto-import="true" default-access="property" default-cascade="none">
<class
name="us.oh.state.dot.jpp.domain.Audit"
table="JPP_AUDIT"
batch-size="100">
<id
name="id"
column="AUDIT_OID"
type="string"
unsaved-value="any">
<generator class="assigned"/>
</id>
<discriminator
column="TARGET_TYPE_TXT"
type="string"
/>
<property
name="auditTime"
column="AUDIT_DT"
/>
<property
name="auditType"
column="AUDIT_TYPE_CD"
/>
<property
name="message"
column="MESSAGE_TXT"
/>
<property
name="userAccountOid"
column="USER_ACCOUNT_OID"
/>
<property
name="username"
column="USER_NME"
/>
<subclass
name="us.oh.state.dot.jpp.domain.JPProjectAudit"
>
<many-to-one
name="jpProject"
column="TARGET_OID"
not-null="true"
cascade="none"
/>
</subclass>
<subclass
name="us.oh.state.dot.jpp.domain.ProjectCategoryAudit"
>
<many-to-one
name="jpProject"
column="PARENT_OID"
not-null="false"
cascade="none"
/>
<many-to-one
name="projectCategory"
column="TARGET_OID"
not-null="false"
cascade="none"
/>
</subclass>
<subclass
name="us.oh.state.dot.jpp.domain.UserAccountAudit"
>
<many-to-one
name="userAccount"
column="TARGET_OID"
not-null="false"
cascade="none"
/>
</subclass>
</class>
</hibernate-mapping>