I have 3 tables
Code:
CREATE TABLE `Person` (
`id` int auto_increment not null primary key,
`name` varchar(75) not null
);
CREATE TABLE `language` (
`id` int auto_increment,
`language` varchar(30) not null,
PRIMARY KEY (`id`)
);
INSERT INTO language (language) VALUES
('Arabic'),('Chinese'),('Dutch'),('English'),('French'),('German'),('Hebrew'),('Hindi'),('Italian'),('Japanese'),('Polish'),('Portuguese'),('Russian'),('Spanish'),('Other');
CREATE TABLE `SPOKEN_LANGUAGES` (
`PERSON_ID` int not null,
`LANGUAGE_ID` int not null,
FOREIGN KEY (`PERSON_ID`) REFERENCES `Person` (`id`),
FOREIGN KEY (`LANGUAGE_ID`) REFERENCES `language` (`id`)
);
A person can speak many languages and a language can be spoken by many people, hence this is a many to many relationship
Person.java
Code:
@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinTable(name = "SPOKEN_LANGUAGES", joinColumns = { @JoinColumn(name = "PERSON_ID") }, inverseJoinColumns = { @JoinColumn(name = "LANGUAGE_ID") })
public Set<Language> getSpokenLanguages() {
return this.spokenLanguages;
}
public void setSpokenLanguages(Set<Language> spokenLanguages) {
this.spokenLanguages = spokenLanguages;
}
Now when I want delete a person it not only deletes the associated rows in the SPOKEN_LANGUAGES table, but also rows from the Language table. Why on earth is it doing this??? It is clear that for a many-many relationship rows from the Language table should not be deleted as they may be related to many rows in the person table!!! Surely this is an issue with the framework?