Hello,
I am currently writing my master thesis and therefore, I had to look
more detailed into the function "hibernate". My current problem is as
follows:
I have a bidirectional manytomany mapping between the entities "person"
and "termin" (german for appointment).
Code:
public class Person {
private Set<Termin> termine = new HashSet<Termin>();
@ManyToMany(mappedBy="personen")
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
public Set<Termin> getTermine() {
return termine;
}
}
public class Termin {
private Set<Person> personen = new HashSet<Person>();
@ManyToMany
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinTable(name="rel_termin_person",
joinColumns={@JoinColumn(name="termin_id",
referencedColumnName="termin_id")},
inverseJoinColumns={@JoinColumn(name="personen_id",referencedColumnName="personen_id")})
public Set<Person> getPersonen() {
return personen;
}
}
The tables in the database are:
table: person
field: personen_id
table: termin
field: termin_id
table: rel_termin_person
field: personen_id
field: termin_id
Now to the problem:
if I delete an appointment (termin), e.g. with
session.delete(Termin.class, ..(1), everything works fine. The
appointment is deleted and the relations in the table rel_termin_person
as well. Entries in the table person are not deleted.
if I, however, delete a person and automatically remove all relations to
that person in the table rel_termin_person as well, then *all*
relations, which are connected to that specific appointment will be
deleted as well - which is clearly *not* wanted. To clear things up: If
there is an appointment, with several related persons, the appointment
will be deleted if a single person is deleted.
I have currently "Cascade" set to "SAVE_UPDATE", which ensures, that I
don't loos all appointments when I am running my tests.
In the end, the workflow should be as follows:
If I delete a person, all relations in rel_termin_person to that
specific person should be deleted. An appointment should, however, not
be deleted, unless there are no other relations to other persons of the
given appointment.
I hope, that my description of the problem is understandable and I thank
all of you in advance for your time and replies.
Sincerely yours,
Roland
P.S.: But now to something completely different ;) Is there anybody out
there, who can explain me the difference between "Hibernate Session" and
"Hibernate EntityManager"? For me, EntityManager is the implementation
of the EJB3 specification. Is this really the only difference?