Hibernate version:
3.0 rc1
Mapping documents:
Code:
<hibernate-mapping package="com.llic.business">
<class name="Document" table="Document" polymorphism="implicit">
<id name="id" column="documentid">
<generator class="native" />
</id>
<property name="contentMgrUniqueKey" column="contentMgrUniqueKey" type="string" not-null="true" />
<property name="dateReceived" column="DATERECEIVED" type="java.util.Date" not-null="true" />
<property name="scanPacketNumber" column="packetnumber" type="string" />
<property name="documentSubTypeID" column="documentSubTypeID" type="java.lang.Long" not-null="true"/>
<many-to-one name="policyNumber" column="policyNumberId" class="PolicyNumber" cascade="save-update"/>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping package="com.llic.business">
<joined-subclass name="Application" extends="Document" table="Application" >
<key column="DocumentID"/>
<!-- needs to be a db generated id or null. setApplicationId() should not be publicly visible -->
<property name="applicationId" column="applicationId" type="java.lang.Long" unique="true"/>
<property name="policyDate" column="policydate" type="java.util.Date" not-null="false" />
<property name="applicationDate" column="applicationDate" type="java.util.Date" not-null="false" />
<set name="appEntityRoles" table="APPENTITYROLE" inverse="true" lazy="false" cascade="all">
<!-- foreign key in appentityrole is application id which references the local property applicationId -->
<key column="applicationId" property-ref="applicationId" />
<one-to-many class="com.llic.business.ApplicationEntityRole" />
</set>
</joined-subclass>
</hibernate-mapping>
I need to generate id’s natively on joined-subclasses for a legacy database.
I have come up with two possible solutions to handle this situation; the first seems to be the best. Any recommendations would be appreciated.
Solution 1:
Utilize the new event system in Hibernate 3. I would like to create a listener on create, that will check for properties such as applicationId. These properties are generated as an Identity by DB2, and thus on the create event I need to utilize the native generator semantics.
I have three questions.
1. Is there some example of a Create event I can look at?
2. Is there some way I can specify that this property needs to be created by the event in the mapping? This will allow me to keep my non-id (to the java side) generated properties mapped with the class in a central location.
3. Is simply retrieving the dialect and calling getIdentityInsertString() enough to get the default insert string? Once I have done this, what is the best way to get the new identities without extending BasicEntityPersister's insert method?
Solution 2:
Extend BasicEntityPersister and JoinedSubclassEntityPersister.
Override insert to retrieve the identity columns (I'll have two on an insert, the mapped Document.id, and Application.applicationId).
This seems like a huge headache, but if someone thinks it would be more efficient, please let me know.
Todd
[/code]