简体   繁体   English

POST 时传递给持久化的分离实体

[英]Detached entity passed to persist when POST

I'm trying to add a join between two tables as I stated in this question: Adding table joins to existing project causes infinite recursion我正在尝试在两个表之间添加连接,正如我在这个问题中所述: 将表连接添加到现有项目会导致无限递归

However now I got a new error which states:但是现在我收到了一个新错误,其中指出:

detached entity passed to persist: sample.todosapp.spring.domain.User] with root cause

When I try to call this code (this is modified method for POST in my REST controller):当我尝试调用此代码时(这是我的 REST 控制器中 POST 的修改方法):

    @RequestMapping(method = POST)
@ResponseStatus(HttpStatus.CREATED)
public Callable<Todo> post(@RequestBody Todo todo) {
    User u = userService.findBySso(SecurityContextHolder.getContext().getAuthentication().getName());

    todo.setUser(u);
    return () -> todoRepository.save(todo);
}

How do you add a join during POST?如何在 POST 期间添加连接?

This is nothring to do with POST.这与 POST 无关。

Its an hibernate exception, and cause of the exception is entity object which you are trying to persist is not in the persistant state.它是一个休眠异常,异常的原因是您尝试持久化的实体对象未处于持久化状态。

  1. Spring JPA -> Please check transaction manager enabled in persistence JPA configuration file. Spring JPA -> 请检查持久性 JPA 配置文件中启用的事务管理器。 Transction method should be marked with @Transactional notation.交易方法应该用@Transactional 符号标记。

    1. Make sure that your were not assinging same id(Primary Key) which is already exists/used in other entity which is already exists in PersistenceContext.确保您没有在 PersistenceContext 中已经存在/使用的其他实体中分配相同的 id(主键)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM