Hello!
I have a n:m relation with Persons and Projects and I have these domain objects.
Person:Code:
@ManyToMany(mappedBy="persons", fetch=FetchType.EAGER)
@Fetch(FetchMode.SELECT)
private List<Project> projects;
Projects:Code:
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name = "Project_Person",
joinColumns = {
@JoinColumn(name="project_id", referencedColumnName="id")
},
inverseJoinColumns = {
@JoinColumn(name="person_id", referencedColumnName="id")
}
)
@Fetch(FetchMode.SELECT)
private List<Person> persons;
Writing a
JUnit test method, I will try to add a Person with two projects, see here:
Code:
@Test
public void testAddPerson() {
List<Person> persons = service.getAllPersons();
Person person = new Person();
person.setEmail("newMail@test.com");
Project testProject = new Project();
testProject.setId(1);
Project testProject2 = new Project();
testProject2.setId(2);
ArrayList<Project> lister = new ArrayList<Project>();
lister.add(testProject);
lister.add(testProject2);
person.setProjects(lister);
service.addPerson(person);
... }
My Dao:Code:
public Person addPerson(Person p) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session sess = sessionFactory.getCurrentSession();
Transaction tx = sess.beginTransaction();
sess.save(p);
sess.flush();
tx.commit();
return p;
}
The problem is: A person can be added, that's fine, but there are not insert on the join table! So Persons are added, but noch linked to a project in the Project_Person table.
I tried with chascading anf those things, but I did not get a solution, so I hop you can help me.
Thanks a lot in advance & Best Regards.