Hello
I will appreciate if someone explain me behavior of my code
I have parent-child , one-to-one relation
@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:
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?