Hi!
I'd like to clear out a doubt.
I have 2 tables with manyToMany relation.
Tables are:
CREATE TABLE operations
(
id_operation serial NOT NULL,
...)
CREATE TABLE skill_types
(
id_skill_type serial NOT NULL,
...)
CREATE TABLE r_operations_skills
(
id_r_operation_skill serial NOT NULL,
id_operation int4 NOT NULL,
id_skill_type int4 NOT NULL)
I mapped the relation as following.
Operation.java
Code:
@ManyToMany(cascade={CascadeType.REFRESH, CascadeType.PERSIST}, fetch=FetchType.LAZY)
@JoinTable(name="r_operations_skills",
joinColumns=@JoinColumn(name="id_operation"),
inverseJoinColumns=@JoinColumn(name="id_skill_type"))
@OrderBy("description")
public List<SkillType> getSkillTypes() {
return skillTypes;
}
SkillTypes.java
Code:
@ManyToMany(cascade={CascadeType.REFRESH, CascadeType.PERSIST}, fetch=FetchType.LAZY, mappedBy="skillTypes", targetEntity=Operation.class)
public List<Operation> getOperations() {
if(operations == null) {
operations = new ArrayList<Operation>();
}
return this.operations;
}
It works as I expect: if I remove a skill used by operation I have an exception. Ok.
My doubt is: why when I remove a skill associated to an operation hibernate removes all entries in the relation tables and then does a lot of inserts?
for example r_operations_skills(id_operation, id_skill_type) contains:
(6,1)
(6,2)
(6,3)
when I remove skill #3 from operation 6 it does:
delete from r_operations_skills where id_operation=6
insert into r_operations_skills (id_operation, id_skill_type) values (6, 1)
insert into r_operations_skills (id_operation, id_skill_type) values (6, 2)
Is it a wrong mapping I made or is this a correct behaviour ?
Thank you
Nicola