나는 엔티티를 저장하기 위해 Spring 데이터 저장소를 사용하고 있지만, 어떤 이유로 cascade가 saveCountryAndNewCity() 테스트에 대해 작동하지 않습니다. 도시는 저장되지 않지만 saveCityAndNewCountry()에 대해 작동합니다. 유사하다. 왜 누군가가 나를 알아낼 수 있습니까? 고마워.스프링 저장소 캐스케이드
public class City {
@Cascade(CascadeType.ALL)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "countryid", nullable = false, insertable = true, updatable = true)
private Country country;
public void setCountry(Country country) {
this.country = country;
country.getCities().add(this);
}
}
public class Country {
@Cascade(CascadeType.ALL)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "country")
private Set<City> cities = new HashSet<City>(0);
public void addCity(City city){
this.cities.add(city);
city.setCountry(this);
}
}
@Test
@Transactional
public void saveCountryAndCity() throws Exception {
Country country = countryRepository.findOneByName("Canada");
City newCity = new City();
newCity.setName("Quebec");
country.addCity(newCity);
countryRepository.save(country);
}
@Test
public void saveCityAndNewCountry() throws Exception {
City city = cityRepository.findOneByName("London");
Country country = new Country();
country.setName("myCountry");
city.setCountry(country);
cityRepository.save(city);
}
1. 스프링 데이터 JPA는 캐스케이드 동작을 담당하지 않습니다. 그것은 JPA 구현 (나는 당신의 경우 최대 절전 모드라고 생각한다)이다. 문제를 더 잘 설명하기 위해 주제와 내용을 더 효과적으로 변경하십시오. –