# Spring Data JPA: Child 'deleteById' is reverted by parent 'findById'

**URL:** <https://discourse.hibernate.org/t/spring-data-jpa-child-deletebyid-is-reverted-by-parent-findbyid/2924>\
**Category:** Hibernate ORM\
**Created:** [July 1, 2019, 7:12pm UTC](https://discourse.hibernate.org/t/spring-data-jpa-child-deletebyid-is-reverted-by-parent-findbyid/2924 "2019-07-01T19:12:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaldo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/michaldo/32/673_2.png) [@michaldo](https://discourse.hibernate.org/u/michaldo)\
**Post date:** [July 1, 2019, 7:12pm UTC](https://discourse.hibernate.org/t/spring-data-jpa-child-deletebyid-is-reverted-by-parent-findbyid/2924/1 "2019-07-01T19:12:21Z")

</div>

Hello

I will appreciate if someone explain me behavior of my code

I have parent-child , one-to-one relation

```java
@Entity
public class Country {

    @Id @GeneratedValue
    private Long id;

    private String name;

    @OneToOne(mappedBy = "country", cascade = CascadeType.ALL)
    private City capital;

---

@Entity
public class City {

    @Id @GeneratedValue
    private Long id;

    private String name;

    @OneToOne(fetch = FetchType.LAZY)
    private Country country;

```

My test code (Spring Data JPA) is:

```auto
new TransactionTemplate(transactionManager).execute(s -> {

  cityRepository.deleteById(parisId);
  //countryRepository.findById(franceId);
  return "ok";
});

System.out.println("Transaction completed");
Country country = countryRepository.findById(franceId).orElseThrow(() -> ...);
System.out.println("Country " + country + " capital: " + country.getCapital());

```

This version prints

> Country 1:France capital: null

And that is something I expect  
But if I uncomment `//countryRepository.findById(franceId)`  
I got this:

> Country 1:France capital: 2:Paris

That is something I do not expect.

Why select (findById) reverts delete (deleteById)?  
What is proper Hibernate usage to avoid that trap?

---

<div class="post-metadata">

**Author:** ![michaldo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/michaldo/32/673_2.png) [@michaldo](https://discourse.hibernate.org/u/michaldo)\
**Post date:** [July 2, 2019, 8:42am UTC](https://discourse.hibernate.org/t/spring-data-jpa-child-deletebyid-is-reverted-by-parent-findbyid/2924/2 "2019-07-02T08:42:23Z")

</div>

In the meantime I found answer for first question.  
It is case known as **un-scheduling entity deletion** - there are several posts SO explaining that
