0

Am using Spring Data JPA and hibernate in as springboot project for persistence, Whenever the method findyById() method on the Repository(JPA CRUD Repository) returns no data for the Primary key for an entity which uses @Version annotation for optimistic locking, it tries to insert an entity to the database.

I could see the insert query generated in the log file.

Has anyone come across such an issue? Please help.

sreenath
  • 1
  • 1
  • 3
  • If it is trying to insert instead of fetching the data then there must be some insert statement, check the link https://stackoverflow.com/questions/42739566/how-to-use-jpa-query-to-insert-data-into-db. – subhashis Mar 17 '22 at 04:32
  • @subhasis, am using spring data jparepository (CRUDRepository) findById method,there is no query defined – sreenath Mar 17 '22 at 04:50
  • 1
    There is too little to go on here. Please show the code, configuration, logging that is happening. – M. Deinum Mar 17 '22 at 06:10
  • @Sreenath please add some more code so that we can analyze. Because this should not happen ideally and is there any jar miss match, please try a clean build (may be there are some code already present in the previous build) – subhashis Mar 19 '22 at 13:33

1 Answers1

0

The things I noticed from your explanation seem very strange to the program because this should not happen, you are just doing a simple query, it should not depend on the output of the query. Consider how much you have to look at different situations in very large application to avoid unexpected behaviors that cause problems.

One of the goals of ORM (Hibernate, etc.) is to ensure that the application meets your needs without worries.

There may be configuration on the side of your existing application that cause this problem. In my opinion, to understand the problem, create another simple project with the minimum requirements, try again.

Ali Mohammadi
  • 76
  • 1
  • 4