spring data jpa 保存报错记录A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: com.htsc.thfx.framework.entity.user.UserInfo.authorities; nested exception

A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: com.htsc.thfx.framework.entity.user.UserInfo.authorities; nested exception is org.hibernate.HibernateException: A collection with cascade

jpa 中 

package com.htsc.thfx.framework.entity.user;

import com.htsc.thfx.framework.entity.menu.Menu;
import com.htsc.thfx.framework.entity.port.ProductCompany;
import com.htsc.thfx.framework.entity.port.ProductCompanyTemporaryStorage;
import com.htsc.thfx.framework.enums.user.UserInfoAuthStatus;
import com.htsc.thfx.framework.enums.user.UserType;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

/**
 * @author K0060021
 * @createtime 2018年7月16日
 * 用户基本信息
 */

@Entity
public class UserInfo implements Serializable {

    @Id
    @Column(length = 50)
    private String username;//用户名



    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "username", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    private Set<UserAuthority> authorities = new HashSet<UserAuthority>();


}

不能只用新的set替换旧的set 需要这样安全删除

            List<UserAuthority> save = userAuthorityDao.save(newList);
            // jpa 替换关联关系
            if (userInfo.getAuthorities() == null)
                userInfo.setAuthorities(new HashSet<>());
            userInfo.getAuthorities().clear();
            userInfo.getAuthorities().addAll(save);

 

posted @ 2019-03-26 14:16  ronniery  阅读(4350)  评论(0编辑  收藏  举报