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