-->
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.  [ 4 posts ] 
Author Message
 Post subject: parent-child issue with composite keys
PostPosted: Wed Nov 19, 2003 3:52 pm 
Newbie

Joined: Wed Nov 19, 2003 3:31 pm
Posts: 4
I've already read documentation, FAQs and previous postings.
However, I've not found this one.

I have a parent-child relationship between two tables, the
foreign table has a composite key which includes the
foreign key to the primary table.

I have a problem while trying to save in cascade. Reads
and deletions work fine. I've found a workaround ( which
I describe later ), but I'd like to know if there's a better way
to to this ).

The schema may not be changed and is like the following:

-- primary table
CREATE TABLE curriculum
(
cu_id INT PRIMARY KEY,
...
);


-- foreign table
CREATE TABLE topic
(
cu_id INT NOT NULL,
to_id INT NOT NULL,
...

PRIMARY KEY ( cu_id, to_id ),
FOREIGN KEY ( cu_id ) REFERENCES curriculum,
);


My mappings look like the following:

For the primary table:

<class table="curriculum"
name="com.acsinet_solutions.curri.data.CurriculumDTO">

<id name="id" type="int" column="cu_id">
<generator class="identity"/>
</id>

<set name="topics" inverse="true" cascade="all-delete-orphan"
lazy="true" sort="natural">
<key column="cu_id"/>
<one-to-many class="com.acsinet_solutions.curri.data.TopicDTO"/>
</set>

</class>


For the foreign table:

<class table="topic" name="com.acsinet_solutions.curri.data.TopicDTO">

<composite-id name="key"
class="com.acsinet_solutions.curri.data.Key">

<key-property name="id" type="int" column="cu_id"/>
<key-property name="number" type="int" column="to_id"/>

</composite-id>

<many-to-one name="curriculum" not-null="true"
insert="false" update="false"
class="com.acsinet_solutions.curri.data.CurriculumDTO">
<column name="cu_id"/>
</many-to-one>

</class>


TopicDTO implements Lifecycle, an interceptor is properly configured
for the session factory, and the key class implements equals() and
hashTable() accordingly.

The problem is that when a save() is tried to be executed, Hibernate
does not automatically set the id property of the Key class, so the
INSERT method is not performed correctly.

The workaround I've found to solve this is to explicitly set the
property as part of the onSave() method:

// snippet of TopicDTO
public boolean onSave( Session arg0 ) throws CallbackException
{

// here I explicitly set the id field of the Key class
getKey().setId( _curriculum.getId() );

_saved = true;

return false;
}


Doing this everything works, but I think this is something the
framework should do for me, so I don't have to explicitly set
the key for every foreign object I have with composite keys
managed this way ( which is VERY common in the type of
projects I work in ).

Maybe I'm missing something in my mappings, I hope someone
could give me a hint.

I've tested this with Hibernate 2.0 and 2.1b6; same results.

- thanks in advance
Santiago


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 7:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you use a composiute-id it is up to you to set the id value before saving.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 7:57 pm 
Newbie

Joined: Wed Nov 19, 2003 3:31 pm
Posts: 4
Thank you for your quick response Gavin

But my point is...

If one of the columns of the composite id is a foreign key
pointing to a primary key automatically generated by
a key generator in Hibernate, and if saves are cascaded,

shouldn't the framework be intelligent enough to detect
this and assign the value of this particular column
instead of requiring me to do that manually ????

Of course I can do it manually and I'll do that to solve
my problem, but I think it REALLY should be done
automatically by the framework. That's why I', using
a framework like Hibernate, to save all the manual
coding it can.

Santiago


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 8:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Not unless you actually *tell* the fwk that that is what it is, by using a <key-many-to-one> mapping.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.