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