-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problems with delete and ManyToMany Relation
PostPosted: Mon May 21, 2007 10:54 am 
Newbie

Joined: Wed Apr 25, 2007 4:02 pm
Posts: 7
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 11:14 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I've found that the delete functionality in h3 is too confusing and time costly to use.

Writing my own delete code to iterate through object trees and delete all associated objects "delete from o where not exist (select * from )" which runs blanket delete statements that run totally in the database side perform much better. It makes concurrent session handling more difficult, but whenever possible, is more reliable.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.