Hi,
Thanks for the reply, I simplified my code for the Forum and added errors. I would like to know if there is any definitive documentation on one-to-one mapping for Hibernate 2.0 ?
I want to model a Purchase Order which has a number of one-to-one mappings and one-to-many. The one to many work but I am having trouble with the one-to-one....
Ideally, I want to add one-to-one child objects to the Purchase Order and have it save with the Purchase Order using the Purchase Order's primary key as the child's primary key.
I am also using xdoclet to generate the hbm files.
For my Purchase Order : -
The class : -
<class
name="au.com.alphawest.po.domain.document.TxPurchaseOrder"
table="TX_PURCHASE_ORDER"
dynamic-update="false"
dynamic-insert="false" >
If Primary Key :-
<id name="id"
column="PO_ID"
type="java.lang.Long"
unsaved-value="null" >
<generator class="sequence">
<param name="sequence">TX_PO_SEQ</param>
</generator>
</id>
Using an Oracle sequence, the un-save value is null - as required
The mapping: -
<one-to-one
name="txPoBuyingParty"
class="au.com.alphawest.po.domain.document.TxPoBuyingParty"
cascade="all"
outer-join="true"
constrained="true" />
I want to save a Purchase Order and automatically save associated objects so cascade is true, the PO might not have a Buying Party so outer join is true and I want the Buying Party to use PO-ID as a foreign key so the releationship is constrained.
The Buying Party is :-
<class
name="au.com.alphawest.po.domain.document.TxPoBuyingParty"
table="TX_PO_BUYING_PARTY"
dynamic-update="false"
dynamic-insert="false"
>
The primary Key is: -
<id name="id"
column="PO_ID"
type="java.lang.Long"
unsaved-value="null" >
<generator class="foreign">
<param name="property">TxPurchaseOrder</param>
</generator>
</id>
I do not want bi-directional mapping but for now I have in the child : -
<one-to-one
name="txPurchaseOrder"
class="au.com.alphawest.po.domain.document.TxPurchaseOrder"
cascade="none"
outer-join="auto"
constrained="false"
/>
The database is create with: -
create table TX_PO_BUYING_PARTY (
[java] PO_ID NUMBER(19,0) not null,
[java] ORG_NAME VARCHAR2(255),
[java] ORG_SHORTNAME VARCHAR2(255),
[java] ORG_GUID VARCHAR2(255),
[java] ORG_ADDRESS_LINE1 VARCHAR2(255),
[java] ORG_ADDRESS_LINE2 VARCHAR2(255),
[java] ORG_SUBURB VARCHAR2(255),
[java] ORG_STATE VARCHAR2(255),
[java] ORG_POST_CODE VARCHAR2(255),
[java] ORG_COUNTRY VARCHAR2(255),
[java] CONTACT_FIRST_NAME VARCHAR2(255),
[java] CONTACT_FAMILY_NAME VARCHAR2(255),
[java] CONTACT_EMAIL VARCHAR2(255),
[java] CONTACT_PHONENO VARCHAR2(255),
[java] CONTACT_ADDRESS_LINE1 VARCHAR2(255),
[java] CONTACT_ADDRESS_LINE2 VARCHAR2(255),
[java] CONTACT_SUBURB VARCHAR2(255),
[java] CONTACT_STATE VARCHAR2(255),
[java] CONTACT_POST_CODE VARCHAR2(255),
[java] CONTACT_COUNTRY VARCHAR2(255),
[java] primary key (PO_ID)
[java] )
AND
[java] alter table TX_PURCHASE_ORDER add constraint FK8092ED8B48CAF5B foreign key (PO_ID) references TX_PO_BUYING_PARTY
If I add just data to the purchase order I get: -
[java] Hibernate: select TX_PO_SEQ.nextval from dual
[java] Hibernate: insert into TRADER.TX_PURCHASE_ORDER (STATE_ID, TRANSMISSION_DATE_TIME, ORDER_NUMBER, ORDER_DATE, PO_TYPE, CONTRACT_TITLE, CONTRACT_NUMBER, PO_RELEASE_NUMBER, ORDER_REQUEST_DATE, REQUESTED_DELIVERY_DATE, ORDER_REQUEST_REFERENCE, CURRENCY_CODE, SHIPPING_REQUIREMENT, TOTAL_TAXES, TOTAL_SHIPPING, TOTAL_MERCHANDISE, TOTAL_ALLOWANCES, TOTAL_LINE_ITEMS, TOTAL_AMOUNT, BUYER_ORG_GUID, BUYER_ORG_NAME, BUYER_ORG_UNIT_GUID, BUYER_ORG_UNIT_NAME, OWNER_GUID, OWNER_NAME, CREATOR_GUID, TRACKING_ID, PAYMENT_METHOD, PAYMENT_DETAILS, PAYMENT_TERMS, MIGRATE, PO_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[java] (util.JDBCExceptionReporter 38 ) SQL Error: 2291, SQLState: 23000
[java] (util.JDBCExceptionReporter 46 ) ORA-02291: integrity constraint (TRADER.FK8092ED8B48CAF5B7DDC2A21) violated - parent key not found
[java] (util.JDBCExceptionReporter 38 ) could not insert: [au.com.alphawest.po.domain.document.TxPurchaseOrder#1]
[java] java.sql.SQLException: ORA-02291: integrity constraint (TRADER.FK8092ED8B48CAF5B7DDC2A21) violated - parent key not found
The key is from another one-to-one mapping done the same as the BUYING_PARTY.
If I add all child objects I get: -
[java] Hibernate: select TX_PO_SEQ.nextval from dual
[java] (hibernate.BaseDAO ? ) unmapped property: TxPurchaseOrder
Any clues on what I am doing wrong or do I have to stop trying to cascade a save and do each child object in turn ?
Thanks
Jon K
|