JPA(有Play!)双向多对多 - 如何从关系的两边删除?

时间:2011-05-18 13:04:17

标签: jpa many-to-many constraints playframework cascading-deletes

我的Play中有以下映射!使用JPA的应用程序:

@Entity
public class Contact extends Model {        
    public String name;

    @ManyToMany(mappedBy = "contacts", fetch = FetchType.EAGER)
    public Set<Category> categories = new HashSet<Category>();

    public void addCategory(Category c) {
        this.categories.add(c);
        if (!c.contacts.contains(this)) {
            c.contacts.add(this);
        }
    }

    @PreRemove
    public void preRemove() {
        for (Category c : this.categories) {
            c.contacts.remove(this);
        }
        this.categories = null;
    }
}

@Entity
public class Category extends Model {
    public String name;

    @ManyToMany
    public Set<Contact> contacts = new HashSet<Contact>();

    public void addContact(Contact c) {
        this.contacts.add(c);
        if (!c.categories.contains(this)) {
            c.categories.add(this);
        }
    }

    @PreRemove
    protected void preRemove() {
        for (Contact c : this.contacts) {
            c.categories.remove(this);
        }
        this.contacts = null;
    }
}

删除类别工作正常,关系正确更新。但是,如果我删除了一个联系人,我就会遇到一个约束违规行为:

Caused by: org.h2.jdbc.JdbcBatchUpdateException: Referential integrity constraint violation: "FK34B085DF487AF903: 
PUBLIC.CATEGORY_CONTACT FOREIGN KEY(CONTACTS_ID) REFERENCES PUBLIC.CONTACT(ID)"; SQL statement:
delete from Contact where id=? [23003-149]

如何确保删除联系人不会删除该类别,但只会删除该关系?

编辑:嗯!问题是我还有一个User对象,它引用了Contact和Category。我错过了清除这种关系。以下是对Contact类中的preRemove()方法的更改:

@PreRemove
public void preRemove() {
    for (Category c : this.categories) {
        c.contacts.remove(this);
    }

    this.user.contacts.remove(this);
    for (Category c : this.user.categories) {
        c.contacts.remove(this);
    }
    //It's important to save the user 
    this.user.save();
}

1 个答案:

答案 0 :(得分:1)

另一种解决方案是在删除主要实体之前从列表中手动删除元素并保存:

while( !contact.categories.isEmpty() ) {  
    contact.categories.remove(0);  
}  
contact.save();  
contact.delete();