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);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM