Hibernate version: 3.0.3
I have been working on this problem for a while, and for some reason I cannot seem to get it properly working properly. What I have been trying to do is have a Parent - Child - Grandchild relationship where everything is cascaded by "all-delete-orphan" up to the "Parent". That way I should only have to call the save() on the Parent (at least that's how I interpreted the documentation as well as the book Hibernate in Action).
I have the following relationship:
TransactionLog----(1 to many)----TransactionGroup----(1 to many)----Transaction
Mapping documents:
TransactionLog.hbm.xml:
Code:
<hibernate-mapping>
<class name="TransactionLog" lazy="false">
<id name="id" column="TRANSACTION_LOG_ID">
<generator class="native"/>
</id>
<set name="transactionGroups" cascade="all-delete-orphan" inverse="true" lazy="true">
<key column="LOG_PARENT_ID"/>
<one-to-many class="TransactionGroup"/>
</set>
</class>
</hibernate-mapping>
TransactionGroup.hbm.xml:
Code:
<hibernate-mapping>
<class name="TransactionGroup" lazy="false">
<id name="id" column="TRANSACTION_GROUP_ID">
<generator class="native"/>
</id>
<many-to-one name="parent" column="LOG_PARENT_ID" not-null="true"/>
<set name="transactions" cascade="all-delete-orphan" inverse="true" lazy="true">
<key column="PARENT_ID"/>
<one-to-many class="Transaction"/>
</set>
</class>
</hibernate-mapping>
Transaction.hbm.xml:
Code:
<hibernate-mapping>
<class name="Transaction" lazy="false">
<id name="id" column="TRANSACTION_ID">
<generator class="native"/>
</id>
<many-to-one name="parent" column="PARENT_ID" not-null="true"/>
</class>
</hibernate-mapping>
Name and version of the database you are using: PostgresQL 8.0
When I am adding a Transaction to a TransactionGroup, the code is something of the following: (Assuming that the code is located in the TransactionGroup class and getTransactions() returns a lazily-initialized Set)
Code:
void addTransaction(Transaction value) {
getTransactions().add(value);
value.setParent(this);
}
The code is similar for the TransactionLog and the TransactionGroup.
The error that I have encountered while trying all of this is that I get: ERROR: insert or update on table "transaction" violates foreign key constraint ...
I am now stuck, and just don't really know how to go about fixing this. Any help would be appreciated.