0

I have a spring data jpa app with Hibernate/Hikari/Hazelcast. The setup is similar to this demo project . The issue I am experiencing in my production code is that when a JpaRepository.save is invoked I get an insert and then an update on the same entity. What I mean is, when we call

parents.save(parent)

Where parents is a repository instance, the generated queries are :
insert into child(id, name, parent_id) values (?,?,?) and immediately after
update child set name=? parent_id=? where id=?,
I fail to see why this is happening. We will be saving around a thousand or more entities at a time and I am trying to minimize the unnecessary DB queries.

I would love it if someone can help out with some ideas why such a behavior is observed. The demo project resembles the actual setup in terms of entity relations, mappings and transactions. open-in-view is set to false, no explicit @Transactional annotations are used.

The problematic behavior is not observed in the demo project. I can't unfortunately share the production code here.

Thanks,

pmanolov
  • 623
  • 1
  • 6
  • 19
  • 1
    Please show your `parent` entity mapping. – SternK Feb 02 '21 at 17:23
  • it's in the [demo project](https://github.com/xmlRoot/spring-data-demo/blob/master/src/main/java/com/example/dbdemo/entities/Parent.java) – pmanolov Feb 02 '21 at 17:31
  • @JensSchauder It actually helped thanks, I managed to fix it, when I removed the cascade=ALL from my relationship in conjunction with the suggestion from the question you've mentioned it started working. In short, removing the cascade and letting the child entity persist/handle the relationship, removed the redundant update statement. – pmanolov Feb 03 '21 at 09:20

0 Answers0