-->
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.  [ 1 post ] 
Author Message
 Post subject: ManyToMany behaviour: a doubt
PostPosted: Fri Mar 16, 2007 1:27 pm 
Beginner
Beginner

Joined: Wed Jun 08, 2005 10:01 am
Posts: 22
Location: Italy
Hi!
I'd like to clear out a doubt.
I have 2 tables with manyToMany relation.

Tables are:
CREATE TABLE operations
(
id_operation serial NOT NULL,
...)


CREATE TABLE skill_types
(
id_skill_type serial NOT NULL,
...)


CREATE TABLE r_operations_skills
(
id_r_operation_skill serial NOT NULL,
id_operation int4 NOT NULL,
id_skill_type int4 NOT NULL)



I mapped the relation as following.

Operation.java

Code:
@ManyToMany(cascade={CascadeType.REFRESH, CascadeType.PERSIST}, fetch=FetchType.LAZY)
    @JoinTable(name="r_operations_skills",
               joinColumns=@JoinColumn(name="id_operation"),
               inverseJoinColumns=@JoinColumn(name="id_skill_type"))
    @OrderBy("description")
   public List<SkillType> getSkillTypes() {
      return skillTypes;
   }




SkillTypes.java

Code:
@ManyToMany(cascade={CascadeType.REFRESH, CascadeType.PERSIST}, fetch=FetchType.LAZY, mappedBy="skillTypes", targetEntity=Operation.class)
   public List<Operation> getOperations() {
       if(operations == null) {
          operations = new ArrayList<Operation>();
       }
      return this.operations;
   }



It works as I expect: if I remove a skill used by operation I have an exception. Ok.

My doubt is: why when I remove a skill associated to an operation hibernate removes all entries in the relation tables and then does a lot of inserts?

for example r_operations_skills(id_operation, id_skill_type) contains:
(6,1)
(6,2)
(6,3)

when I remove skill #3 from operation 6 it does:

delete from r_operations_skills where id_operation=6
insert into r_operations_skills (id_operation, id_skill_type) values (6, 1)
insert into r_operations_skills (id_operation, id_skill_type) values (6, 2)

Is it a wrong mapping I made or is this a correct behaviour ?

Thank you
Nicola


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

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.