2

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

    @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?

Community
  • 1
  • 1
s1ddok
  • 4,615
  • 1
  • 18
  • 31
  • I dont exactly understand the problem beween post and join (it's 2 totaly differen concept) but for your error you need to use saveOrUpdate() method – jpprade Mar 11 '16 at 19:09

2 Answers2

2

remove cascade = CascadeType.ALL from relationship JPA @ManyToOne with CascadeType.ALL

Community
  • 1
  • 1
Hammad
  • 101
  • 1
  • 10
0

This is nothring to do with 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. Transction method should be marked with @Transactional notation.

    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.
Shaan
  • 588
  • 1
  • 4
  • 15