I was working with a ManyToMany relation. But I have got problem while adding new records in the Association Table.
In fact I am working with three tables: the Person table is related to table Project using an association table lt_person_project.
Using ORM, the generated entities are :
In the Person Entity (it is the owner of this relation):
@ManyToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinTable(name = "lt_person_project", schema = "enterprise_schema", joinColumns = { @JoinColumn(name = "person_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "project_id", nullable = false, updatable = false) })
public List getProjects() {
return this.projects;
}
In the Project Entity, we Have:
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "projects")
public List getPersons() {
return this.persons;
}
Now I have created the person, and while doing it, I have assigned to him some projects. I wanted then to assign him new projects, so the code was:
@Transactional(propagation= Propagation.REQUIRES_NEW)
public void assignProjectsToPerson(String personId, List projectIds) {
Person person = this.findPerson(personId);
List projects = projectDao.findProjects(projectIds);
person.setProjects(projects);
entityManager.merge(person);
}
Doing so, Hibernate will first execute a query where it Delete all the rows in the association table where the personId is the one given by our method. Then it inserts the NEW records.
Thinking about my code, it is normal that I have such behavior, as I have told hibernate to setProjects. So I have ignored the old ones.
So in order to tell Hibernate that In fact I am adding new Records in the Association table, I have changed my setProject with this line:
person.getProjects().addAll(projects);
Even using this line, Hibernate will first delete the records then put them again, but at least, it will add the old and new ones.
In fact I am working with three tables: the Person table is related to table Project using an association table lt_person_project.
Using ORM, the generated entities are :
In the Person Entity (it is the owner of this relation):
@ManyToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinTable(name = "lt_person_project", schema = "enterprise_schema", joinColumns = { @JoinColumn(name = "person_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "project_id", nullable = false, updatable = false) })
public List
return this.projects;
}
In the Project Entity, we Have:
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "projects")
public List
return this.persons;
}
Now I have created the person, and while doing it, I have assigned to him some projects. I wanted then to assign him new projects, so the code was:
@Transactional(propagation= Propagation.REQUIRES_NEW)
public void assignProjectsToPerson(String personId, List
Person person = this.findPerson(personId);
List
person.setProjects(projects);
entityManager.merge(person);
}
Doing so, Hibernate will first execute a query where it Delete all the rows in the association table where the personId is the one given by our method. Then it inserts the NEW records.
Thinking about my code, it is normal that I have such behavior, as I have told hibernate to setProjects. So I have ignored the old ones.
So in order to tell Hibernate that In fact I am adding new Records in the Association table, I have changed my setProject with this line:
person.getProjects().addAll(projects);
Even using this line, Hibernate will first delete the records then put them again, but at least, it will add the old and new ones.