-->
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 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  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 8:26 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 2:43 pm
Posts: 29
Location: Silicon Valley
There is a useful discussion on the consequences of how you map collections when it comes to updating their contents (eg by removing a skill associated with an operation):

http://www.hibernate.org/hib_docs/v3/re ... ollections

Looking at your schema, your collection might be following 'bag' semantics, ie a given skill could be associated with a given operation more than once (whether or not you do so, as far as hibernate can tell from your mapping, it's possible). From the documentation:

Quote:
Bags are the worst case. Since a bag permits duplicate element values and has no index column, no primary key may be defined. Hibernate has no way of distinguishing between duplicate rows. Hibernate resolves this problem by completely removing (in a single DELETE) and recreating the collection whenever it changes. This might be very inefficient.


So the behavior is correct if you really want bag semantics. To make it more efficient, try either adding an index column to your join table so hibernate can treat it as an actual list (as declared in your java), or explicitly declare the collection as a Set, so hibernate knows each element is unique. Either way, it won't have to delete and reinsert the whole collection when you change one element, like it's doing now.


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.