1

I ran into some weird issue:

I have a table Constrainable and a table Attribute. Within the Attribute table i specify to which Constrainable the Attribute belongs with a foreign key constraint. I added a CASCADE ON DELETE to that foreign key constraint in the Attribute table.

Now if i want to delete the Attribute the Constrainable is being deleted too.. This should not be happening or am I wrong? I am using this method to do so:

public void remove(IDBObject obj) throws DBException {
    if (manager != null) {
        IDBObject o = null;
        try {
            o = manager.getReference(obj.getClass(), obj.getPrimaryKey());
        } catch (EntityNotFoundException e) {
            throw new DBException("Entity doesnt exist");
        }
        manager.getTransaction().begin();
        manager.remove(o);
        manager.getTransaction().commit();
        return;
    }
    throw new DBException("Manager is closed or null");
}

What could the reasons be for this behaviour?

More detailed outline of the DB:

Constrainable-Table:

| ID |

Attribute-Table:

| ID | Constrainable-ID | Value | <--- Here is the CASCADE ON DELETE defined

2 Answers 2

2

That is impossible, and as you have described correctly, it should not be happening. Since computers rarely lie there must be a mistake in configuration provoking this behaviour.

  • check the foreign key is correctly defined:

    ALTER TABLE Attribute-Table ADD CONSTRAINT FK_attr_constr FOREIGN KEY (Constrainable-ID) REFERENCES Constrainable-Table (ID) ON DELETE CASCADE ON UPDATE NO ACTION;
    
  • check Constrainable-Table table does not reference anywhere attribute table, or other tables which might end up referencing attribute. (if you have more than one CASCADE be careful with how those cascades propagate through your schema)

  • lastly remove the ON DELETE CASCADE and see if the same behavior happens, since the cascade should happen when you remove the referenced row from Contrainable-table the deletion of an attribute should have no effect on Constrianable-table.

As a general rule, avoid CASCADE when possible unless you are 100% sure about it because maybe another developer will find this problem when working on another thing if he was not aware of the CASCADE.

5
  • I was messing around a little with the cascade and without cascade and it seems as that it is still removed even WITHOUT the cascade. I removed ALL cascades from the db and it still happens... Kinda starting to think this is an issue with eclipselink generating stupid SQL
    – Rittel
    Aug 28, 2013 at 13:48
  • if you remove all the cascades and still happens, something is very very wrong. More checklist: are you correctly removing the child object and not the father, are there any active triggers that might do that?
    – Daren
    Aug 28, 2013 at 13:53
  • I am removing the Attribute Object - Thus the child object and it should never be removing the father object then. It does though.. Also have no triggers defined that might do so. Currently doing some small tests with cascading enabled and disabled and seeing if i get differing behaviour with things in the db
    – Rittel
    Aug 28, 2013 at 14:11
  • There is something we must be missing, but that should never be the expected behaviour and it is too huge to be a bug from the library. When you find what was wrong please don't hesitate to answer, now I'm curious as to what is wrong. :)
    – Daren
    Aug 28, 2013 at 14:16
  • Yea, can't really be a bug, but must be a fault of mine. I will post a solution as soon as i found one
    – Rittel
    Aug 28, 2013 at 14:21
1

Ok i finally figured out what is going on:

Eclipselink generated some faulty Entity classes in which every Foreignkey Relationship had a (cascade = CascadeType.ALL). After i removed the cascades in the classes where they shouldn't be everything worked fine.

Well that happens if zou are too lazy to write the classes on your own :P I would say one could be expecting that the classes are generated without errors like this. Well I learned my lessons now

1
  • 1
    Ahaha! I've never worked with eclipselink... eclipse java and mysql a lot, but I tend to have a Do it Yourself approach to everyhitng. Dam the law of leaky abstraction joelonsoftware.com/articles/LeakyAbstractions.html you need to understand very deeply what you are working with in case you find an unexpected leak... +1 for this answer, cuz it might useful if it ever happens to others.
    – Daren
    Aug 28, 2013 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.