ребята, я пробовал разные методы, предложенные здесь или на других сайтах, чтобы исправить эту ошибку, я исправил ее раньше, но в другом случае, но этот кажется сложным. сущность с именем департамент, которая имеет отношение oneToMany к себе, поэтому департамент может иметь один или несколько подотделов, а департамент должен иметь один и только один родительский отдел.
Подразделение отдела:
@Entity
@Table(name = "DEPARTMENTS")
public class Department {
@Id
@Column(name = "dep_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long depId;
private String depName;
@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "supDep", referencedColumnName = "dep_Id")
@JsonIgnoreProperties(value ={"departments","users"} , allowSetters = true)
private Department supDep;
@OneToMany(mappedBy = "supDep", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnoreProperties(value ={"supDep","users"} , allowSetters = true)
private List<Department> departments = new ArrayList<>() ;
Constructors & getters &setters...
}
DepartmentRepository:
@Repository
public interface DepartmentRepository extends JpaRepository<Department,Long> {
Department findByDepName(String name);
}
DepartmentService Interface:
public interface DepartmentService {
Department add(Department department);
Department update(Department department, Long id);
void delete(long id);
List<Department> findAll();
Department findByName(String name);
Department findById(Long id);
User getChefDep(Long idDep);
}
DepartmentServiceImplement:
@Service(value = "departmentService")
public class DepartmentServiceImpl implements DepartmentService {
....
@Override
public Department add(Department department) {
Department newDep = new Department();
if(department.getDepId() != 0)
newDep.setDepId(department.getDepId());
newDep.setDepName(department.getDepName());
newDep.setChefDep(department.getChefDep());
newDep.setSupDep(department.getSupDep());
newDep.setDepartments(department.getDepartments());
newDep.setUsers(department.getUsers());
return departmentRepository.save(department);
}
...
}
DepartmentController ADD метод:
@RestController
@CrossOrigin("*")
@RequestMapping("/department/")
public class DepartmentController {
...
@PostMapping("add")
public Department add(@RequestBody Department department) {
return departmentService.add(department);
}
...
}
в любом случае, когда я добавляю новый отдел с почтальоном, он работает, и отдел сохраняется в базе данных:
{
"depName": "marketing",
"supDep": null,
"departments": []
}
, и когда я добавляю новый отдел с supDep, который не ' он не существует в базе данных, он тоже работает, и оба объекта сохраняются в базе данных:
{
"depName": "Security",
"supDep":
{
"depName": "IT",
"supDep": null,
"departments": [],
"chefDep": 0,
}
}
, но когда я добавляю новый отдел, передавая supDep, отдел, который существует:
{
"depName": "sub-marketing",
"supDep":
{
"depId": 1,
"depName": "marketing"
}
}
it выдает эту надоедливую ошибку:
{
"timestamp": "2020-03-17T14:49:40.071+0000",
"status": 500,
"error": "Internal Server Error",
"message": "detached entity passed to persist: com.ats.remotetimemanager.Model.Department; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.ats.remotetimemanager.Model.Department",
"trace": "org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.ats.remotetimemanager.Model.Department; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.ats.remotetimemanager.Model.Department\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:319)\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)\r\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)\r\n\tat org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)\r\n\tat org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)\r\n\tat org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178)