I'm retrofitting hibernate to a somewhat arcane table per sub-class, manual, pre-hibernate application.
I've done this using subclasses, joins and discriminators.
The outline of my problem is as follows:
I have the superclass DiaryItem mapped to table DIARYITEM, primary key DIARYID
A subclass of the DiaryItem, DiaryAction maps to table DIARYACTION, with its primary key DIARYID being the foreign key of DIARYITEM
A subclass of the DiaryItem, DocumentRequest maps to table DIARYDOCUMENT, with its primary key DIARYID being the foreign key of DIARYITEM
The complication that I cannot map is as follows:
DiaryAction has on it the following
private DocumentRequest documentRequest;
so in otherwords, a DiaryAction can have a DocumentRequest associated with it.
In the existing applicatiion, what gets written to the database when a new DiaryItem with a DocumentRequest is created are the following
1 DIARYITEM record
1 DIARYACTION record with key = DIARYID of DIARYITEM
1 DIARYDOCUMENT record with key = DIARYID of DIARYITEM
How do I map the DocumentRequest property on DiaryAction ? I've tried ONE-TO-ONE and it ended up writing a brand new DIARYITEM record to hold the superclass bit of the DocumentRequest, when I don't want it to bother, as the superclass field values are identical to the ones on DiaryAction.
I know this is all bizarre and mad, but its what I am stuck with. Any ideas, apart from a dark room and a straight jacket ?
Hibernate version:
3
Mapping documents:
<?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>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="bizarre.mapping.diary.DiaryItem" table="DIARYITEM">
<id name="identifier" type="integer" column="DIARYID" unsaved-value="-1">
<generator class="native" />
</id>
<discriminator type="string" formula="trim(TYPE)" />
<property name="active" type="int" column="ACTIVEINACTIVEFLAG" length="1" />
<property name="amendedDate" type="java.sql.Timestamp" column="AMENDEDDATE" length="19" />
<property name="amendedUser" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="AMENDEDUSER" length="10" />
<property name="authorityLevel" type="int" column="AUTHORITYLEVEL" length="2" />
<property name="creationDate" type="java.sql.Timestamp" column="CREATIONDATE" length="19" />
<property name="creationUser" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="CREATIONUSER" length="10" />
<property name="parentId" type="big_decimal" column="PARENTID" length="9" />
<property name="itemStatus" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="STATUS" length="20" />
<property name="priority" type="int" column="PRIORITY" length="2" />
<property name="itemSummary" type="java.lang.String" column="ITEMSUMMARY" length="60" />
<property name="itemType" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="TYPE" length="10" />
<property name="entityId" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="ENTITYID" length="60" />
<property name="entityType" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="ENTITYTYPE" length="10" />
<!-- bi-directional one-to-many association to Diarytext -->
<list name="text" lazy="false" inverse="true" cascade="all">
<key>
<column name="DIARYID" />
</key>
<list-index> <column name="SEQUENCENUMBER"/></list-index>
<one-to-many class="bizarre.mapping.diary.DiaryText" />
</list>
<subclass name="bizarre.mapping.diary.Note" discriminator-value="NOTE"></subclass>
<subclass name="bizarre.mapping.diary.LogEntry" discriminator-value="LOG"></subclass>
<subclass name="bizarre.mapping.diary.Condition" discriminator-value="CONDITION">
<join table="DIARYACTION">
<key column="DIARYID">
</key>
<property name="conditionType" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="CONDITIONTYPE" length="10" />
<property name="reviewDate" type="java.sql.Timestamp" column="REVIEWDATE" length="19" />
<property name="reviewUser" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="REVIEWUSER" length="10" />
<property name="completionDate" type="java.sql.Timestamp" column="COMPLETIONDATE" length="19" />
<property name="completionUser" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="COMPLETIONUSER" length="10" />
<property name="mandatory" type="bizarre.mapping.util.datatypes.CustomBooleanType" column="MANDATORYFLAG" length="1" />
</join>
</subclass>
<subclass name="bizarre.mapping.diary.DiaryAction" discriminator-value="ACTION">
<one-to-one name="documentRequest" cascade="all" lazy="false" ></one-to-one>
<join table="DIARYACTION">
<key column="DIARYID">
</key>
<property name="reviewDate" type="java.sql.Timestamp" column="REVIEWDATE" length="19" />
<property name="reviewUser" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="REVIEWUSER" length="10" />
<property name="completionDate" type="java.sql.Timestamp" column="COMPLETIONDATE" length="19" />
<property name="completionUser" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="COMPLETIONUSER" length="10" />
<property name="mandatory" type="bizarre.mapping.util.datatypes.CustomBooleanType" column="MANDATORYFLAG" length="1" />
</join>
</subclass>
<subclass name="bizarre.mapping.diary.DocumentRequest" discriminator-value="DOCUMENT">
<join table="DIARYDOCUMENT">
<key column="DIARYID">
</key>
<property name="name" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="DOCUMENTNAME" length="40" />
<property name="documentNumber" type="int" column="DOCUMENTNUMBER" length="9" />
<property name="numberOfCopies" type="bizarre.mapping.util.datatypes.CustomIntType" column="NUMBEROFCOPIES" length="9" />
<property name="documentPath" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="SAVEDDOCID" length="128" />
<property name="documentStatus" type="bizarre.mapping.util.datatypes.CustomStringTrimType" column="STATUS" length="20" />
</join>
</subclass>
</class>
</hibernate-mapping>