-->
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: Composite Key : Problem with update
PostPosted: Wed Oct 25, 2006 1:01 am 
Newbie

Joined: Wed Oct 25, 2006 12:47 am
Posts: 2
I am facing a problem, which I dont know how to handle. I am using a table which has a composite key, lets say C1 and C2. The key C1 and C2 are also foreign key to another table.

In the application it is possible the value of C2 key can be changed for a particular record ( for example, in case of Parent-Child relationship the Parent can be changed ). When I try to update the record using saveOrUpdate method then i get StaleObjectStateException.

I assume that the StaleObjectStateException is not occuring because of versioning as I am the only user of the application.

This seems to be happening because I changed one of the composite key (C2) value and when the Hibernate's saveOrUpdate tries to find the record during update it can't find any matching record in the database which has the old value of C1 and new value of C2.

Does this mean that if one of the keys in a composite key changes during modification by the user, then we should delete any previous records (with original composite key) in the database and insert a new one (with modified composite key) ??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 3:20 am 
Newbie

Joined: Tue Oct 24, 2006 5:50 pm
Posts: 3
you don't need delete any previous records , you must be defined into your mapping's files the atribute cascade="save-update", besides, the key in the two tables C1 and C2 must be assigned by you, for that you cant define the atribute class="assigned"

here an example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping schema="public" package="beans">
<class name="Estudiante" table="estudiante" schema="public" optimistic-lock="none">
<id name="solapin_e" type="integer" unsaved-value="null" column="solapin_e">
<generator class="assigned"/>
</id>
<property name="nombre" type="string" column="nombre"/>

<set name="profesores" table="estudiante_profesor" inverse="true" cascade="save-update">
<key foreign-key="solapin_p" column="solapin_p"/>
<many-to-many entity-name="beans.Profesor" foreign-key="solapin_e" column="solapin_e"/>
</set>
</class>
</hibernate-mapping>


the class atribute is the way that the DB will be generate the keys of the diferents records in your's tables, if you put it in assigned, you should be changes any key, and update it when you desire


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 3:58 am 
Newbie

Joined: Wed Oct 25, 2006 12:47 am
Posts: 2
I think the example that you are giving is when there is no composite key. In guess in my case, composite key modification is resulting in problems. Let me explain again:

I have a table 'Customer'. The 'Customer' table has a composite key. The two columns that make up the composite key of 'Customer' table are 'ID' and 'Zip_Code'. The 'ID' is a foreign key to 'Customer_ID' table and 'Zip_Code' is foreign key to 'Global_Zip_Code' table.

In the DAO code I create a 'Customer' object and save it -- Everything goes fine and the 'Customer' table is updated.

Scenario 1 : I read the newly created 'Customer' object from the database and change the 'Zip_Code' ( noe that this is part of the composite key of 'Customer' table) and try to update the 'Customer' table -- I get StaleObjectStateException

Scenario 2: I read the newly created 'Customer' object from the database and make changes to Customer object but I dont change the Zip_Code, and I save the changes -- The database is updated correctly.

The above two scenarios give me a feeling that
Hibernate work something like this:

1. When UPDATING the record, hibernate attempts to update the matching record in the database which has the Zip_Code and ID of Customer object on which update method was invoked.

2. Hibernate doesn't find any matching record in the database because I changed the Zip_Code ( in Scenario 1). So no record update happens and Hibernate throws StaleObjectStateException.

Correct me if I am wrong.

Does this mean that I can never UPDATE a record with a modified composite key value ?

If anyone needs more clarification on the question then please let me know.

thanks
Ashish Sarin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:06 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Quote:
Does this mean that I can never UPDATE a record with a modified composite key value ?


A record is identified by its key. If you are changing the key itself, it would mean a NEW record and not an update of the existing record.


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.