JDO Delete parent row without deleting the children rows

173 views Asked by At

I need a bit of help trying to delete a row from the parent table but without deleting the children. I have this table MyEntityA. That has 2 foreign keys to table MyEntityB like:

@PersistenceCapable(detachable = "true")
public class MyEntityA implements Serializable {

    private static final long serialVersionUID = 3575973891490133579L;

    /*
     * KEYS
     */
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.UUIDHEX)
    @Column(name = “Id", jdbcType = "VARCHAR", length = 32)
    private String Id;

    @Persistent(defaultFetchGroup = "true", dependent = "true")
    @Column(name = "CURRENTCLIENT", jdbcType = "VARCHAR", length = 32)
    @ForeignKey(name = “MY_ENTITY_B_FK1", deleteAction = ForeignKeyAction.RESTRICT, table = “MY_ENTITY_B", columns = {
            @Column(name = “ID") }, updateAction = ForeignKeyAction.RESTRICT)
    private MyEntityB currentClient;

    @Persistent(defaultFetchGroup = "true", dependent = "true")
    @Column(name = "RELATEDTO", jdbcType = "VARCHAR", length = 32)
    @ForeignKey(name = “MY_ENTYTY_B_FK2", deleteAction = ForeignKeyAction.RESTRICT, table = "MY_ENTITY_B", columns = {
            @Column(name = “ID") }, updateAction = ForeignKeyAction.RESTRICT)
    private MyEntityB relatedTo;


    public Relationship() {
    }


    public MyEntityB getCurrentClient() {
        return currentClient;
    }

    public void setCurrentClient(MyEntityB currentClient) {
        this.currentClient = currentClient;
    }

    public MyEntityB getRelatedTo() {
        return relatedTo;
    }

    public void setRelatedTo(MyEntityB relatedTo) {
        this.relatedTo = relatedTo;
    }

}

Now, using JDO, when I had tried to delete a row from the table MyEntityA like:

PersistenceManager pm = …
MyEntityA objectById = pm.getObjectById(MyEntityA.class,”abc”);
pm.deletePersistent(objectById)

Or like:

PersistenceManager pm = …
MyEntityA objectById = pm.getObjectById(MyEntityA.class,”abc”);
objectById.setRelatedTo(null);
objectById.setCurrentClient(null);
pm.makePersistent(objectById);
pm.deletePersistent(objectById)

in both cases, the code will remove the row from table MyEntityA and the rows from the table MyEntityB, that is being referenced in the table MyEntityA. I wish I could delete the row from MyEntityA but to don't touch the rows from the table MyEntityB.

Can somebody help me?

1

There are 1 answers

0
Greşanu Emanuel - Vasile On

I had found the correct solution. In my relationship class to my foreign keys, I had to change the line

@Persistent(defaultFetchGroup = "true", dependent = "true")
to
@Persistent(defaultFetchGroup = "true", dependent = "false")

This is the post that had enlightened me: Is there any method like orphanRemoval of JPA in JDO with Kodo?

Also, the official documentation: https://db.apache.org/jdo/api30/apidocs/javax/jdo/annotations/Persistent.html#dependent()