-->
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.  [ 3 posts ] 
Author Message
 Post subject: Troubles persisting many-to-many association
PostPosted: Wed Jun 25, 2008 3:11 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Hibernate version: 3.0.5

Mapping documents:
Code:
<class name="be.rmi.intranet.db.users.OrganizationalUnit" table="RH_OUNIT" lazy="false">
  <id name="id">
   <column name="OUNIT_ID" not-null="true" unique="true" sql-type="NUMBER"/>
      <generator class="sequence">
       <param name="sequence">RH_GENERIC_SEQ</param>
      </generator>
  </id>
   <property name="key" column="OUNIT_KEY" type="string" not-null="false"/>
   <property name="nameFr" column="OUNIT_NAME_FR" type="string" not-null="false"/>
   <property name="nameNl" column="OUNIT_NAME_NL" type="string" not-null="false"/>
    <set name="members" table="RH_EMP_OUNIT_LINK" cascade="all">
      <key column="OUNIT_ID" not-null="true"/>
       <many-to-many class="be.rmi.intranet.rh.Employee" column="EMP_ID"/>
   </set>
</class>


Code:
public class OrganizationalUnit {

    private long id;
    private String nameFr;
    private String nameNl;
    private String key;
    private Set members = new HashSet();
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    // ......
    public Set getMembers() {
        return members;
    }
    public void setMembers(Set members) {
        this.members = members;
    }



Code between sessionFactory.openSession() and session.close():

Code:
//Get an "Employee", get and OrganziationUnit by id,
ounit.getMembers().add(newEmployee);
Transaction tx = session.beginTransaction();
session.update(ounit);
tx.commit();
//Note: using long running session in webapp environnment, session is associated with requesting user and reattached each time. That explain why you don't see the load() codes.


Name and version of the database you are using:Oracle 10

The generated SQL (show_sql=true):
Code:
Hibernate: select organizati0_.OUNIT_ID as OUNIT1_, organizati0_.OUNIT_KEY as OUNIT2_37_, organizati0_.OUNIT_NAME_FR as OUNIT3_37_, organizati0_.OUNIT_NAME_NL as OUNIT4_37_ from RH_OUNIT organizati0_


Hello,
i have trouble persisting a many-to-many association. As you can see from mapping we use an association table, we then add() items to the association Set, but upon calling session.update() with the Hibernate object containing the Set, the Set is not persisted to the database.

As you can see from log, no update is done to take into account the add() call. However, debuggin with eclipse shows that ounit.getMembers() returns a PersistentSet, which mean hibernate should persist it's content during call to update (or not? at least it proves it handles the Set). Note that
1) replacing update with saveOrUpdate() has no more effect
2) calling update(newEmployee) does not have effect either
3) changing a property of ounit (like the nameFr), has the effect of ounit being updated to database, but not the association (nameFr is changed, association table remains untouched)
4) calling session.flush() has no effect either
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 10:15 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
One option might be to set the CascadeType to ALL. The other option might be to ensure you save both sides of the relationship.

Here's a neat WIKI article on the subject of many-to-many relationship persistence.

http://en.wikibooks.org/wiki/Java_Persistence/Relationships

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Troubles persisting many-to-many association
PostPosted: Thu Oct 13, 2011 11:37 am 
Newbie

Joined: Thu Sep 29, 2011 5:15 pm
Posts: 2
You should update the other end of the bi-directional association as well, if you want the association to be persisted. Refer to this:

http://stackoverflow.com/questions/7750549

(Found the answer from the previous poster's link)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.