Hello, faced with an interesting problem. According to the documentation, the save method for SpringBoot can either save a new object or edit an already saved one. The problem is the following: when I try to edit an object, springboot edits this object and then duplicates the old one and also saves it. As you can see from the example, the object that I am editing is first located using id For example,

 Question qq = questionRepository.findQuestionById(id); qq.setPicture(filename); questionRepository.save(qq); 

Gives as a result in a database:

 id: 1, name: test, picture: testpicture 

and

 id: 2, name: test, picture: NULL 
  • There you need to use the method method update (qq) and not save (qq) - Nikolay Belyakov
  • docs.spring.io/spring-data/commons/docs/current/api/org/ ... According to the documentation, this method stores a new object in the database, rather than updating the old one. - aleshka-batman
  • Nikolay, as far as I can see, there is no update method at all. - Julia
  • @ aleshka-batman, stackoverflow.com/questions/43719828/… . CrudRepository has only save but it acts as update as well. When you do save on entity with existing id it will do an update I know for sure that save performs two functions - Julia
  • And in the link that you have attached it is also written about it - Julia

1 answer 1

The error turned out to be very stupid. I set the cascade.ALL annotation and saved two objects at the same time: parent and child. in the end, child was saved twice.