I have a Java class that contains a list of another class.
@Entity public class Country {
private Long id;
private List<Hotel> hotels;
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="COUNTRY_SEQ")
@SequenceGenerator(name="COUNTRY_SEQ", sequenceName="COUNTRY_SEQ", allocationSize=1)
public Long getId() {
return id;
}
public void setHotels(List<Hotel> hotels) {
this.hotels = hotels;
}
@OneToMany
@JoinTable(
name="COUNTRY_HOTELS",
joinColumns=@JoinColumn(name="COUNTRY_ID"),
inverseJoinColumns=@JoinColumn(name="HOTEL_ID")
)
public List<Hotel> getHotels() {
return hotels;
}
}
When I try to delete a Country, I get “ORA-02292: integrity constraint (HOT.fk1a1e72aaf2b226a) violated – child record found” because it can’t delete a Country when its children (=Hotels) still exist.
However, it is MEANT to be like this! I don’t want to my Hotels deleted when I delete a Country.
I tried without any @Cascade-annotation but it failed. I also tried with SAVE_UPDATE, still failed.
So which @Cascade-annotation do I need (or is there another solution?):
- PERSIST
- MERGE
- REMOVE
- REFRESH
- DELETE
- SAVE_UPDATE
- REPLICATE
- DELETE_ORPHAN
- LOCK
- EVICT
Bart
You have to remove the hotels from the list of hotels of the country before deleting the country, in order to tell Hibernate that the hotels don’t have a country anymore.