休眠双向ManyToMany删除问题 [英] Hibernate Bidirectional ManyToMany delete issue

查看:102
本文介绍了休眠双向ManyToMany删除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我为用户和公司提供了实体:

In my project I have entities for users and companies:

@Entity
@Table(name = "users")
public class UserDetails {

    @Id
    @GeneratedValue
    @Column(name = "user_id")
    private int id;

    @Column(name = "first_name")
    @NotEmpty
    @Size(min = 2, max = 20)
    private String firstName;

    @ManyToMany(cascade = CascadeType.REFRESH)
    @JoinTable(name = "users_companies",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "company_id"))
    private Set<CompanyDetails> userCompanies = new HashSet();

    //getters and setters of course...
}

@Entity
@Table(name = "companies")
public class CompanyDetails {
    @Id
    @GeneratedValue
    @Column(name = "company_id")
    private int id;

    @Column(name = "name")
    @NotEmpty
    @Size(min = 1, max = 255)
    private String name;

    @ManyToMany(mappedBy = "userCompanies")
    private Set<UserDetails> companyUsers = new HashSet();

}

我将用户分配给公司,然后尝试删除。当我删除一个用户时,一切正常 - 用户删除,'users_companies'表中的记录也被删除,公司仍然存在(如有必要)。但是,当我试图删除一家公司时,我有以下根源导致的堆栈跟踪:

I assign user to company in view and then try to delete. When I delete a user everything is ok - user deleted, record from 'users_companies' table deleted too and company remains (as necessary). But when I try to delete a company I have stack trace with following root cause:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`d_torianik/users_companies`, CONSTRAINT `FK447D806437A764EB` FOREIGN KEY (`company_id`) REFERENCES `companies` (`company_id`))

你能帮我解决这个问题吗?谢谢。

Could you help me resolve this issue? Thank you.

推荐答案

我知道这是旧的,但它可能有助于某人......我试图做同样的事情 - 从每个主表中删除,先删除连接表中的引用记录,然后从主表中删除记录。 benzonico的帖子是有效的,但有一个更简单的方法来做到这一点(不必自己从联合表中删除记录)。公司表中的映射需要更改为主表(不使用mappedBy):

I know this is old but it may help someone... I was trying to do the exact same thing - deleting from each main table to delete the referenced records first in the joined table and then delete the record from the main table. benzonico's post is valid but there is a more simple way to do this (without having to remove the records from the joined table yourself). The mapping at companies table needs to be changed to be a main table as well (don't use mappedBy):

@Entity
@Table(name = "companies")
public class CompanyDetails {
    @Id
    @GeneratedValue
    @Column(name = "company_id")
    private int id;

    @Column(name = "name")
    @NotEmpty
    @Size(min = 1, max = 255)
    private String name;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "users_companies",
    joinColumns = {@JoinColumn(name = "company_id")},
    inverseJoinColumns = @JoinColumn(name = "user_id"))
    private Set<UserDetails> companyUsers = new HashSet();

}

这应该是个诀窍。现在,当你删除一家公司时,Hibernate将首先删除users_companies中的记录,然后它将删除公司本身。更多信息: http://www.codereye .com / 2009/06 / hibernate-bi-directional-many-to-many.html

That should do the trick. Now whenever you delete a company, Hibernate will first delete the records in users_companies and then it will delete the company itself. More info here: http://www.codereye.com/2009/06/hibernate-bi-directional-many-to-many.html

这篇关于休眠双向ManyToMany删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆