-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Cascading Nested collection with composite key
PostPosted: Sun May 20, 2007 5:16 pm 
Newbie

Joined: Thu Dec 23, 2004 6:08 am
Posts: 4
In our legacy schema, we have a two level master slave association with composite primary key layered as follows:

Dept: id (generated by sequence)
Div: (dept_id, div_id): div_id is a list index
Unit: (dept_id, div_id, unit_id) unit_id is again a list index

My first impulse is to model this association as a nested collection of components, but since the composite-element does not allow another collection, I try to model them as collection of entities with composite key. Here are my mappings:

Dept:

<hibernate-mapping>
<class name="Department" table="DEPT">
<id name="id" column="ID">
<generator class="sequence">
<param name="sequence">DEPT_SEQ</param>
</generator>
</id>
<property name="name" column="NAME"/>
<list name="divisions" table="DIVISION" cascade="save-update">
<key column="DEPT_ID" />
<list-index column="DIV_ID"/>
<one-to-many class="Division" />
</list>
</class>
</hibernate-mapping>

Div:

<hibernate-mapping>
<class name="Division" table="DIVISION">
<composite-id >
<key-property name="deptId" column="DEPT_ID"/>
<key-property name="divisionId" column="DIV_ID"/>
</composite-id>
<property name="name" column="NAME"/>
</class>
</hibernate-mapping>

(Unit is not included, since just Dept-Div already fails)

This follows the example in section 8.4 of the reference manual, except the relation is unidirectional. The cascade is save-update. Hibernate does not populate composite key and so I populate divId myself. I cannot populate deptId in Division, because the value is generated by the sequence and is not known beforehand.

When I try to save the department object, I got this exception:

Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("REALESTATEBOARD"."DIVISION"."DEPT_ID")

at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:498)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:12410)

The SQL parameter spit out with debug turned on is:

Hibernate:
insert
into
DIVISION
(NAME, DEPT_ID, DIV_ID)
values
(?, ?, ?)
16:27:23,065 DEBUG StringType:80 - binding 'development' to parameter: 1
16:27:23,066 DEBUG LongType:73 - binding null to parameter: 2
16:27:23,067 DEBUG LongType:80 - binding '2' to parameter: 3

This shows that with cascade save-update, dept_id is not populated by hibernate. Do I have to switch to use cascade="none" and then populate the deptId field mannually?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 3:42 am 
Newbie

Joined: Thu Dec 23, 2004 6:08 am
Posts: 4
I found the solution in this thread:

http://forum.hibernate.org/viewtopic.ph ... ey+cascade

The solution is handled by the key-many-to-one tag. This is my final mapping(for one level):

<class name="Department" table="DEPT">
<id name="id" column="ID">
<generator class="sequence">
<param name="sequence">DEPT_SEQ</param>
</generator>
</id>
<property name="name" column="NAME"/>
<set name="divisions" table="DIVISION" cascade="all,delete-orphan" inverse="true">
<key column="DEPT_ID" />
<one-to-many class="Division"/>
</set>
</class>

<class name="Division" table="DIVISION">
<composite-id name="id" class="DivisionKey">
<key-many-to-one name="department" column="DEPT_ID" class="Department"/>
<key-property name="divisionId" column="DIV_ID"/>
</composite-id>
<property name="name" column="NAME"/>
</class>

DivisionKey contains a Department field and a divisionId field.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.