When these cascades of user questions mails are sent to all
on the mailing list everybody just gets to delete them.

so, go to user forum. This mail is for development of hibernate, not usage.

/max


> Hi,
>
> I have defined a many to many relationship between two classes, viz.
> Event and Person (defined in a separate database table person_event).
> The code of these classes is listed at the end. Now, suppose I want to
> delete a person, so all its related associations with events must also
> get deleted from the person_event table. In other words, I want cascade
> ON DELETE.
>
> Lets consider a scenario.
> -  The "events" table contains three events identified by id=1, 2, 3.
> - The "person" table contains two persons identified by id=4, 5.
> - The "person_event' table containing associations like 1-4, 2-4, 3-5
>
> Now, suppose I delete event 1 using Hibernate.delete(), then not only
> does it delete event1, and association person_event1-4, but also the
> person4 and subsequently, person_event2-4 and event2.
>
> I dont understand why this is happening. Could someone please explain
> where I am committing a mistake.
>
> Thanks in advance
>
> - Abhishek
>
> Event and Person Class
>
> package test;
>
> import java.io.Serializable;
> import java.util.Date;
> import java.util.HashSet;
> import java.util.Set;
>
> import javax.persistence.CascadeType;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.ManyToMany;
> import javax.persistence.Table;
> import javax.persistence.Temporal;
> import javax.persistence.TemporalType;
>
> @Entity
> @Table(name="events")
> public class Event implements Serializable {
>     private Long id;
>     private String title;
>     private Date date;
>
>     public Event() {
>     }
>
>     @Id @GeneratedValue(strategy=GenerationType.AUTO)
>     public Long getId() {
>         return id;
>     }
>
>     private void setId(Long id) {
>         this.id = id;
>     }
>
>     @Temporal(TemporalType.TIMESTAMP)
>     public Date getDate() {
>         return date;
>     }
>
>     public void setDate(Date date) {
>         this.date = date;
>     }
>
>     public String getTitle() {
>         return title;
>     }
>
>     public void setTitle(String title) {
>         this.title = title;
>     }
>    private Set<Person> participants = new HashSet<Person>();
>
>     @ManyToMany(
>             cascade={CascadeType.ALL},
>             mappedBy="events",
>             targetEntity=Person.class
>         )
>     public Set<Person> getParticipants() {
>         return participants;
>     }
>
>     public void setParticipants(Set<Person> participants) {
>         this.participants = participants;
>     }
> }
>
> package test;
>
> import java.io.Serializable;
> import java.util.HashSet;
> import java.util.Set;
>
> import javax.persistence.CascadeType;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.JoinColumn;
> import javax.persistence.JoinTable;
> import javax.persistence.ManyToMany;
> import javax.persistence.Table;
>
> @Entity
> @Table(name="person")
> public class Person implements Serializable {
>    private Long person_id;
>     private int age;
>     private String firstname;
>     private String lastname;
>
>     public Person() {}
>
>     @Id @GeneratedValue(strategy=GenerationType.AUTO)
>     public Long getId() {
>         return person_id;
>     }
>
>     public void setId(Long id) {
>         this.person_id = id;
>     }
>
>     public int getAge() {
>         return age;
>     }
>
>     public void setAge(int age) {
>         this.age = age;
>     }
>
>     public String getFirstname() {
>         return firstname;
>     }
>
>     public void setFirstname(String firstname) {
>         this.firstname = firstname;
>     }
>
>     public String getLastname() {
>         return lastname;
>     }
>
>     public void setLastname(String lastname) {
>         this.lastname = lastname;
>     }
>
>     private Set<Event> events = new HashSet<Event>();
>
>     @ManyToMany(
>             targetEntity=Event.class,
>             cascade={CascadeType.ALL}
>             )
>     @JoinTable(
>             name="person_event",
>             [EMAIL PROTECTED](name="person_id")},
>             [EMAIL PROTECTED](name="event_id")})
>     // Defensive, convenience methods
>     protected Set<Event> getEvents() {
>         return events;
>     }
>
>     protected void setEvents(Set<Event> events) {
>         this.events = events;
>     }
>
>     public void addToEvent(Event event) {
>         this.getEvents().add(event);
>         event.getParticipants().add(this);
>     }
>
>     public void removeFromEvent(Event event) {
>         this.getEvents().remove(event);
>         event.getParticipants().remove(this);
>     }
> }
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job  
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache  
> Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> hibernate-devel mailing list
> hibernate-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel



-- 
--
Max Rydahl Andersen
callto://max.rydahl.andersen

Hibernate
[EMAIL PROTECTED]
http://hibernate.org

JBoss a division of Red Hat
[EMAIL PROTECTED]

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
hibernate-devel mailing list
hibernate-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to