Duplicate Identity found when creating new Object with Castor JDO

140 Views Asked by At

I have a Servlets web app using Castor (version 1.1.2.1) mappings and JDO. When I try to create a new object that has a m:1 relation, I get a DuplicateIdentityException for the parent object.

org.exolab.castor.jdo.DuplicateIdentityException: Duplicate identity found for object of type Parent with identity <1(1)>: an object with the same identity already exists in persistent storage.

My Parent object is loaded in a filter and set in the request.

Filter

Database db = getDatabase():
db.begin();
request.setAttribute("parent", db.load(Parent.class, 1));
db.commit();
db.close();

Servlet

Parent parent = (Parent) request.getAttribute("parent");
Database db = getDatabase();
db.begin();
new Child();
child.setParent(parent);
db.create(child); <-- Exception here: DuplicateIdentityException
db.commit();
db.close();

getDatabase calls JDOManager.getDatabase();

Mapping file:

<class name="Parent" identity="id" key-generator="IDENTITY">
    <cache=type="fifo" />
    <field name="id" type="integer">
        <sql name="id" type="integer" />
    </field>
    <field name="child" type="Child" collection="arraylist">
        <sql many-key="parent_id" />
    </field>
</class>

<class name="Child" identity="id" key-generator="IDENTITY">
    <cache=type="fifo" />
    <field name="id" type="integer">
        <sql name="id" type="integer" />
    </field>
    <field name="parent" type="Parent">
        <sql name="parent_id" />
    </field>
</class>

How can a create the Child object with the correct relationship to Parent using Castor JDO?

0

There are 0 best solutions below