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