300 words

Foreign key constraint issue

2017-02-21 19:59


I'm trying to create two tables (Parent and Child) with a foreign key constraint from Child to Parent, so that when the parent is deleted all children are removed as well. Unfortunately, in one specific case that foreign key constraint doesn't apply and I can't figure how to make it work.

The definitions of the classes I have are as follows:

public class Child {
    private long id;

    @ManyToOne(optional = false, cascade = {})
    @JoinColumn(name="PARENT_ID", nullable = false, referencedColumnName = "id")
    private Parent parent;

    public Child(Parent parent) {this.parent = parent;}

public class Parent {
    private long id;

    @OneToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "parent", orphanRemoval = true)
    List<Child> children;

I also have Spring repositories defined for the two classes.

Now, in the following case not all children are deleted:

Parent parent = new Parent();

Child child1 = new Child(parent);

parent = parentRepository.findOne(...); //After this operation Parent.children is populated with child1 as expected.

Child child2 = new Child(parent);
childRepository.save(child2); // New child referring to the parent is persisted

parentRepository.delete(parent); // Here trying to delete the parent along with all children referring to it.

After the above is executed, only child1 is deleted and child2 remains. It seems that only the entities that are currently populated in the parent's list get deleted, whereas I'd want to remove all orphaned child entities (as with a foreign key constraint). Interestingly enough, if I issue delete of the parent without retrieving it first (in which case the list of children is null), both children get deleted.

Can you please help me out?

k's picture
Joined on 2017-02-21
User Post #1
2017-02-24 09:55

Try keeping the child-parent bidirectional relationship up to date all the time, as according to JPA it is the application resposibility (when there are changes). So when a child is instantiated and it references a parent, you should also update the list in the parent to include the new child.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support's picture
Joined on 2010-05-03
User Post #2,706

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Maximum file size: 32 MB