持久性异常外键约束在休眠中失败

发布于 2025-01-09 23:03:54 字数 813 浏览 0 评论 0原文

我在 hibernate 中定义了以下两个类

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
 
}

@Entity
public class PhoneNumber {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @ManyToOne(cascade = CascadeType.ALL)
    private Person person;
 
}


当我保留电话号码对象或人员对象时,它会被正确插入。 但当我这样做时

 Person person = session.get(Person.class,1);
        session.remove(person);
        transaction.commit();

I get the foreign key violation exception. But since I have declared a column as ManyToOne shouldn't hibernate automatically delete the corresponding phonnumber records?
I am not sure if I need to add any extra code to do that

I have defined the following two classes in hibernate

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
 
}

@Entity
public class PhoneNumber {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @ManyToOne(cascade = CascadeType.ALL)
    private Person person;
 
}

When I persist a phone number object or a person object it's getting inserted properly.
But when I do

 Person person = session.get(Person.class,1);
        session.remove(person);
        transaction.commit();

I get the foreign key violation exception. But since I have declared a column as ManyToOne shouldn't hibernate automatically delete the corresponding phonnumber records?

I am not sure if I need to add any extra code to do that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2025-01-16 23:03:54

您具有双向关系,这就是为什么您也必须在 Person 中添加 PhoneNumber 。并使用mappedBy属性来表明Person是反面,每当删除它时,请同时删除所有电话号码。

像这样:

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;

    @OneToMany(mappedBy="person", cascade = CascadeType.ALL)
    private Set<PhoneNumber> phoneNumbers;
 
}

查看了解更多信息。

You have a bi-directional relationship, that is why you have to add the PhoneNumbers in your Person too. And use the mappedBy attribute to show that the Person is the inverse side and whenever it is deleted, please delete every phone number also.

Like this:

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;

    @OneToMany(mappedBy="person", cascade = CascadeType.ALL)
    private Set<PhoneNumber> phoneNumbers;
 
}

Check this for more information.

抹茶夏天i‖ 2025-01-16 23:03:54

首先,您必须在两个类中定义 Person 和 PhoneNumber 之间的关系。

抛出错误是因为电话号码取决于您删除的人员对象。
如果添加@OneToMany,您还可以定义属性cascade = CascadeType.ALL,然后所有电话号码都会被删除。

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
    private List<PhoneNumber> phoneNumbers;
}

@Entity
public class PhoneNumber {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    private Person person;

}

First of all, you have to define the relationship between Person and PhoneNumber in both classes.

The error is thrown because there are phone numbers depending on the person object you removed.
If you add @OneToMany you can define also the property cascade = CascadeType.ALL and then all phone numbers are removed consequently.

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
    private List<PhoneNumber> phoneNumbers;
}

@Entity
public class PhoneNumber {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    private Person person;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文