简体   繁体   English

Hibernate自引用关系外键约束问题

[英]Hibernate self referenced relation foreign key constraint problem

I am setting up a Springboot API which uses hibernate to communicate with the database. 我正在设置一个Springboot API,它使用hibernate与数据库进行通信。 I have a User table, and each user should be able to follow other users. 我有一个User表,每个用户都应该能够关注其他用户。 I am not experienced with hibernate, so after a while I got something that seemed to work, except that I cannot delete a user if that specific user is being followed by other user(s). 我对hibernate没有经验,所以过了一段时间我得到了似乎有用的东西,除非我不能删除用户,如果该特定用户被其他用户跟踪。 This is because of a foreign key constraint. 这是因为外键约束。

This is what the user looks like: 这是用户的样子:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true)
    @JsonProperty
    private int id;

    @Column(unique = true)
    @JsonProperty
    private String email;

    @Column
    @JsonProperty
    private String hashedSaltedPassword;

    @Column
    @JsonProperty
    private String salt;

    @Column
    @JsonProperty
    private String firstName;

    @Column
    @JsonProperty
    private String lastName;

    @JsonProperty
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "User_Follows", schema = "todo", joinColumns=@JoinColumn(name="user_id"))
    private List<User> follows;

This creates the user table and a seperate table called User_Follows. 这将创建用户表和名为User_Follows的单独表。 This table has the user_id and a follows_id, which is the userId that is being followed. 此表具有user_id和following_id,即正在遵循的userId。 This is pretty much what I want. 这几乎是我想要的。

Deleting a user works perfectly as long as that user isn't being followed by other users (so as long as the userId of that user isn't in the follows_id column). 只要该用户没有被其他用户跟踪,删除用户就可以完美地工作(只要该用户的userId不在follow_id列中)。

But it gives the following error if it is: 但如果它是:它会给出以下错误:

The DELETE statement conflicted with the REFERENCE constraint "FKc38v25qxktm9b0r7lg1s2t1tl". The conflict occurred in database "dbi390100_db", table "todo.User_Follows", column 'follows_id'.

This should not happen, what I want is that all rows where the userId exists of the User_Follows table will be deleted whenever the user is being deleted. 这不应该发生,我想要的是每当用户被删除时,User_Follows表的userId存在的所有行都将被删除。

Is there a solution for this? 这有解决方案吗?

There are two ways of accomplishing what you want. 有两种方法可以完成你想要的。

  • Define a mappedBy property with another field to let hibernate know about the entire relationship, something like: 使用另一个字段定义mappedBy属性,让hibernate知道整个关系,例如:
@ManyToMany(targetEntity = User.class, mappedBy = "follows")
private List<User> followers = new ArrayList<>();

You can have a look at this other question 你可以看看这个其他问题

  • Or you can create a @PreRemove method in your user class to manually delete all references of your User: 或者,您可以在用户类中创建@PreRemove方法,以手动删除用户的所有引用:
@PreRemove
private void removeFromFollowers() {
   for (User user : followers) {
       user.removeFollowed(this);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM