-->
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.  [ 7 posts ] 
Author Message
 Post subject: Changing a collection "off-session" and saving.
PostPosted: Sun Jan 18, 2004 8:17 am 
Newbie

Joined: Tue Dec 16, 2003 9:57 am
Posts: 13
Hi,
I'm using Hibernate 2.1.1, Windows 2000, Jdk 1.3 and DB2 7.2. I am trying to do the following:

I have a Case object with a set of Involved objects (persons involved in the case). From a web application, I create, changes and retrieves Cases. All saving and retrival happens i a stateless session bean, so the session used when saving is different from the session uses when retrieving a Case.

I have run into trouble when I try to change the set of Involved objects. Here's a use case:

1. Retrieve a Case from the storage. The case has two Involved objects.
2. Remove the to Involved objects, and add a new one.

When saving, a new Case object is created (in the web layer) and persisted (in the ejb layer). An update is issued, since the id is set. However, the Case has now 3 Involved objects and not 1. The two old ones are not removed.

Another use case:
1. Retrieve a Case from the storage. The case has two Involved objects.
2. Change one of the Involved.

This result in the following error from the JDBCExceptionReporter:
[IBM][CLI Driver][DB2/NT] SQL0803N One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "INVOLVED" from having duplicate rows for those columns. SQLSTATE=23505

Here's my mappings (only the important parts):
Code:
<hibernate-mapping auto-import="false">
<class name="Case" table="CASE">
  <id name="id" type="long" column="ID">
   <generator class="identity"/>
  </id>
  <set name="involved" table="INVOLVED" inverse="true" cascade="all-delete-orphan">
   <key column="CASEID"/>
   <one-to-many class="Involved"/>
  </set>       
</class>
</hibernate-mapping>

<hibernate-mapping auto-import="false">
<class name="Involved" table="INVOLVED">
  <composite-id unsaved-value="any">
   <key-property name="id"/>
   <key-many-to-one name="sak" column="CASEID"/>
  </composite-id>       
</class>
</hibernate-mapping>


The database is set up like this:
Code:
CREATE TABLE CASE
(ID INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1, NO CACHE ) NOT NULL,
   [...]
);

ALTER TABLE CASE
ADD CONSTRAINT id PRIMARY KEY (ID);

CREATE TABLE INVOLVED
(ID VARCHAR(11) NOT NULL,
   CASEID INTEGER NOT NULL,
   [...]
);

ALTER TABLE INVOLVED
ADD CONSTRAINT id_caseId PRIMARY KEY (ID, SAKID);
 
ALTER TABLE INVOLVED
ADD CONSTRAINT INVOLVERT_CASE FOREIGN KEY (CASEID)
REFERENCES CASE(ID);


Is it possible to change a collection (both the number of members, and the attributes of a member). Outside the session which retrieved the holding component? When I do it inside the same session, everything seems fine.

Hope you can help me out.

Regards,
Ola


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 18, 2004 8:20 am 
Newbie

Joined: Tue Dec 16, 2003 9:57 am
Posts: 13
Additional information:

I've overridden the equals() and hashMap() methods in the Involved class. To objects are equal if id and caseid of the two are the samen.

- Ola


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 18, 2004 9:14 am 
Newbie

Joined: Tue Dec 16, 2003 9:57 am
Posts: 13
Note to self:

the following thread seems to be the same issue:

http://forum.hibernate.org/viewtopic.php?t=926960


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 18, 2004 9:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Quote:
Is it possible to change a collection (both the number of members, and the attributes of a member).


1. Yes. But all-delete-orphan works only when the elements are removed within the session. So you have to delete removed elements manually somehow.

2. Yes. Most likely your unsaved-value mapping is wrong, resutling in a call to INSERT instead of UPDATE.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 2:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
gloeglm wrote:
1. Yes. But all-delete-orphan works only when the elements are removed within the session. So you have to delete removed elements manually somehow.

Not sure of that, but for sure, the underlying collection must be the SAME : Hibernate flag the deletion undercover thanks to it's own implementation.
As Michael said object.setList(someOtherList) won't work.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 4:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hmm, I thought I had read this somewhere in the manual, can't find it now however. I will try this sometimes today and try to confirm.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 5:56 am 
Newbie

Joined: Tue Dec 16, 2003 9:57 am
Posts: 13
Hi again,
I'm trying to implement the Interceptor pattern - but have run into troubles as explained in this thread:

http://forum.hibernate.org/viewtopic.php?t=927190

- Ola


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