简体   繁体   English

无法删除具有多对一关系的父实体

[英]Cannot delete parent entity with a ManyToOne relationship

I'm developing a web application with Java, Spring and JPA.我正在使用 Java、Spring 和 JPA 开发 Web 应用程序。 I have these two entities, which represent a user and its follow notifications.我有这两个实体,它们代表一个用户及其关注通知。 When a user follows another user, the one who gets followed, receives a follow notification.当用户关注另一个用户时,被关注的用户会收到关注通知。

@Entity
public class User{
    @Id
    private Long userId;

    @OneToMany(mappedBy = "user",
            cascade = CascadeType.ALL,
            orphanRemoval = true)
    private List<Notification> notifications;
}

@Entity
public class FollowNotification{
    @Id
    private Long followNotificationId;

    @ManyToOne(
            fetch = FetchType.LAZY
    )
    @JoinColumn(name = "id")
    private User user;

    @ManyToOne
    private User follower;

}

I want to delete User and delete also all its notifications, but I want to keep the referenced user of the notification, which is the one who started following the main user.我想删除 User 并删除它的所有通知,但我想保留通知的引用用户,即开始关注主要用户的用户。 The only problem is that when I try to delete the user I get this error:唯一的问题是,当我尝试删除用户时,出现此错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails ( database_name . follow_notification , CONSTRAINT FK8ob9ie1tjkkon036uokkg52j2 FOREIGN KEY ( follower_id ) REFERENCES user ( user_id )) com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:不能删除或更新父行,外键约束失败( database_namefollow_notification ,约束FK8ob9ie1tjkkon036uokkg52j2外键( follower_id )参考useruser_id ))

What am I doing wrong?我究竟做错了什么? How can I delete a User with all its notification keeping the other side of the relationship?如何删除一个用户,其所有通知都保留在关系的另一端?

you have foreign key constraint follower_id which ensures you can not delete user which is referenced by notifications..you need to set referenced user_id to null for all notifications for a given user on many to one side before deleting the user.你有外键约束 follower_id 确保你不能删除被通知引用的用户..在删除用户之前,你需要将引用的 user_id 设置为多对一侧给定用户的所有通知为空。

i)remove orphanRemoval = true as well as CascadeType.ALL(CascadeType.ALL, which includes REMOVE) indicates that remove operations should be cascaded automatically to entity objects that are referenced by that field (multiple entity objects can be referenced by a collection field)) in User i)remove orphanRemoval = true 以及 CascadeType.ALL(CascadeType.ALL, 其中包括 REMOVE) 表示移除操作应该自动级联到该字段引用的实体对象(一个集合字段可以引用多个实体对象) ) 在User

@OneToMany(mappedBy = "user") private List<Notification> notifications ; @OneToMany(mappedBy = "user") private List<Notification> notifications

ii)as well as add nullable = true for user in FollowNotification ie ii) 以及在FollowNotification ie 中为用户添加nullable = true

@JoinColumn(name = "id",nullable = true) private User user;

iii)Now code to delete the user would look something like this iii)现在删除用户的代码看起来像这样

public void deleteUser(int id) {
 // code to get entity manager and then      
Query query = entitymanager.createQuery("Select u " + "from User u " + "where u.userId=+id");

      User user=query.getResultList( ).get(0);

      Set notifications= user.getNotification();
      Iterator<Notification> noteIterator = notifications.iterator();
        while (notification.hasNext()){
            Notification notification= noteIterator.next();
            notification.setUser(null); 
            } 
      entitymanager.remove(user);
    }

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

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