해결된 질문
작성
·
573
0
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: io.security.corespringsecurity.domain.entity.Role; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: io.security.corespringsecurity.domain.entity.Role
찾아보니까
@ManyToOne(cascade = {CascadeType.ALL},fetch = FetchType.LAZY)
해당 에러가 발생 합니다.
RoleHierarchy 의 경우
package io.security.corespringsecurity.domain.entity;
import lombok.*;
import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name="ROLE_HIERARCHY")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(exclude = {"parentName", "roleHierarchy"})
@Builder
public class RoleHierarchy implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(name = "child_name")
private String childName;
@ManyToOne(cascade = {CascadeType.ALL},fetch = FetchType.LAZY)
@JoinColumn(name = "parent_name", referencedColumnName = "child_name")
private RoleHierarchy parentName;
@OneToMany(mappedBy = "parentName", cascade={CascadeType.ALL})
private Set<RoleHierarchy> roleHierarchy = new HashSet<RoleHierarchy>();
}
해당 내용으로 작성 하였구요
SetupDataLoader에서
setupSecurityResources()
=>
private void setupSecurityResources() {
Set<Role> roles = new HashSet<>();
Role adminRole = createRoleIfNotFound("ROLE_ADMIN", "관리자");
roles.add(adminRole);
createResourceIfNotFound("/admin/**", "", roles, "url");
createUserIfNotFound("admin", "pass", "admin@gmail.com", 10, roles);
Set<Role> roles1 = new HashSet<>();
Role managerRole = createRoleIfNotFound("ROLE_MANAGER", "매니저");
roles1.add(managerRole);
createResourceIfNotFound("io.security.corespringsecurity.aopsecurity.method.AopMethodService.methodTest", "", roles1, "method");
createResourceIfNotFound("io.security.corespringsecurity.aopsecurity.method.AopMethodService.innerCallMethodTest", "", roles1, "method");
createResourceIfNotFound("execution(* io.security.corespringsecurity.aopsecurity.pointcut.*Service.*(..))", "", roles1, "pointcut");
createUserIfNotFound("manager", "pass", "manager@gmail.com", 20, roles1);
createRoleHierarchyIfNotFound(managerRole, adminRole);
Set<Role> roles3 = new HashSet<>();
Role childRole1 = createRoleIfNotFound("ROLE_USER", "회원");
roles3.add(childRole1);
createResourceIfNotFound("/users/**", "", roles3, "url");
createUserIfNotFound("user", "pass", "user@gmail.com", 30, roles3);
createRoleHierarchyIfNotFound(childRole1, managerRole);
}
해당 내용으로 작성 하였습니다.
보고 따라치는 과정에서 무언가 빠졌을까요
답변 2
0
2022. 04. 07. 00:41
Account entity 의
@ManyToMany(fetch = FetchType.LAZY, cascade={CascadeType.MERGE})
@JoinTable(name = "account_roles", joinColumns = { @JoinColumn(name = "account_id") }, inverseJoinColumns = {
@JoinColumn(name = "role_id") })
private Set<Role> userRoles = new HashSet<>();
로 변경 하여 해결 하였습니다.