Hi,
I'm running into the following problem which has me somewhat baffled being new to Hibernate. I'll try to keep this short, I have the following situation:
DocumentHeader
| 1
|
| 1
CommitmentDocument
| 1
|
| *
CommitmentLineItem
Looks innocent enough eh? CommitmentDocument extends DocumentHeader, so from the application perspective only a CommitmentDocument object exists, however it's persisted to two distinct tables, doc_header_t and commitment_doc_t. In effect a one to one mapping exists between the two, using a shared primary key (fdocNbr). CommitmentDocument in turn has a collection of line items (List of CommitmentLineItem). The CommitmentDocument object shares the same primary key (fdocNbr) with the CommitmentLineItems, CommitmentLineItems also has a sequence within the scope of the document number (fdocNbr) and together with the document number this sequence makes out the primary key (composite) for the CommitmentLineItem.
The mapping looks something like this (abbreviated):
<class name="DocumentHeader" table="DOC_HEADER_T">
<id name="fdocNbr" type="string" unsaved-value="null">
<column name="FDOC_NBR" not-null="true" length="36"/>
<generator class="PaddedSequenceGenerator">
<param name="sequence">document_number</param>
<param name="total_width">5</param>
</generator>
</id>
<joined-subclass name="CommitmentDocument" table="COMMITMENT_DOC_T">
<key column="FDOC_NBR">
</key>
</joined-subclass>
<bag
name="lineItems"
inverse="true"
lazy="false"
order-by="FDOC_LINE_NBR"
cascade="all">
<key column="FDOC_NBR"/>
<one-to-many class="CommitmentLineItem"/>
</bag>
</class>
<class
name="CommitmentLineItem"
table="COMMITMENT_LINE_T"
>
<composite-id name="comp_id" class="CommitmentLineItemPK">
<key-many-to-one
name="fdocNbr"
class="CommitmentDocument"
column="FDOC_NBR"
/>
<key-property
name="fdocLineNbr"
column="FDOC_LINE_NBR"
type="java.lang.Integer"
length="7"
/>
</composite-id>
</class>
Now to test this I do the following, create a new DommitmentDocument, save (session.save) it (to get the doc number back), the I load it (session.load), then I create a new CommitmentLineItem and CommitmentLineItemPK objects and add them to the loaded document, and then try to save the whole thing again:
String newDocNumber = pcd.saveDocument(cd);
CommitmentDocument cd2 = pcd.loadDocument(newDocNumber);
pcd.printDocument(cd2);
item1.setDocument(cd2);
cd2.getLineItems().add(item1);
pcd.saveDocument(cd2);
It works until I try to save the document again with the lineItems, I get the following exception:
[java] IllegalArgumentException occurred calling getter of com.imi.hsos.document.fcom.persistence.DocumentHeader.fdocNbr
[java] net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.imi.hsos.document.fcom.persistence.DocumentHeader.fdocNbr
[java] at net.sf.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:110)
.
.
.
.
[java] Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
I guess my question is two fold, does the mapping look alright and any ideas/pointers on why this exception gets thrown ? The model is driven from the ERD basically, it's fairly fixed and I don't have much chance of having it changed.
Many thanks!
Peter
|