-->
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: @ManytoMany flaw?
PostPosted: Sat Oct 10, 2009 5:13 pm 
Newbie

Joined: Thu Aug 20, 2009 1:12 pm
Posts: 11
I have 3 tables
Code:
CREATE TABLE `Person` (
   `id` int auto_increment not null primary key,
   `name` varchar(75) not null
);

CREATE TABLE `language` (
  `id` int auto_increment,
  `language` varchar(30) not null,
  PRIMARY KEY  (`id`)
);
INSERT INTO language (language) VALUES

('Arabic'),('Chinese'),('Dutch'),('English'),('French'),('German'),('Hebrew'),('Hindi'),('Italian'),('Japanese'),('Polish'),('Portuguese'),('Russian'),('Spanish'),('Other');

CREATE TABLE `SPOKEN_LANGUAGES` (
  `PERSON_ID` int not null,
  `LANGUAGE_ID` int not null,
  FOREIGN KEY (`PERSON_ID`) REFERENCES `Person` (`id`),
  FOREIGN KEY (`LANGUAGE_ID`) REFERENCES `language` (`id`)
);

A person can speak many languages and a language can be spoken by many people, hence this is a many to many relationship

Person.java
Code:
@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinTable(name = "SPOKEN_LANGUAGES", joinColumns = { @JoinColumn(name = "PERSON_ID") }, inverseJoinColumns = { @JoinColumn(name = "LANGUAGE_ID") })
    public Set<Language> getSpokenLanguages() {
        return this.spokenLanguages;
    }

    public void setSpokenLanguages(Set<Language> spokenLanguages) {
        this.spokenLanguages = spokenLanguages;
    }


Now when I want delete a person it not only deletes the associated rows in the SPOKEN_LANGUAGES table, but also rows from the Language table. Why on earth is it doing this??? It is clear that for a many-many relationship rows from the Language table should not be deleted as they may be related to many rows in the person table!!! Surely this is an issue with the framework?


Top
 Profile  
 
 Post subject: Re: @ManytoMany flaw?
PostPosted: Sat Oct 10, 2009 5:21 pm 
Newbie

Joined: Thu Aug 20, 2009 1:12 pm
Posts: 11
right I have removed cascade = CascadeType.ALL and its now working as I want it to i.e. rows from person and spoken_languages tables are deleted but language rows remain intact. When I removed the cascade = CascadeType.ALL I was still expecting the rows in the spoken_languages table to be there (i thought i would probably have to remove these manually...) so i'm not really sure whats going on, but its doing what I want so i cant complain really!!!


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.