-->
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.  [ 8 posts ] 
Author Message
 Post subject: Why does update() not cascade to many-to-many table?
PostPosted: Wed Jun 25, 2008 3:16 am 
Senior
Senior

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

Mapping documents:
Code:
<class name="mypack.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="mypack.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 an OrganziationUnit by id,
// content of both objects show datas comes from database (ie not empty new objects)
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

I explored doc of hibernate about many-to-many association, there doesn't seem to be any information in there about this problem. Help greatly appreciated.
[/code]


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

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's a WIKI article on the subject. It's rather pertinent.

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:
PostPosted: Wed Jun 25, 2008 10:24 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Thanks, but it was completly unrelated. Our webapplication had 2 jars. In one of them the mapping shiwed above was present , in the other one, it was an old non working mapping (no cascade, inverse=true, baaaad). And of course, the classloader was using the incorrect one. Mapping was correct, it was just not read :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 10:24 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Thanks, but it was completly unrelated. Our webapplication had 2 jars. In one of them the mapping shiwed above was present , in the other one, it was an old non working mapping (no cascade, inverse=true, baaaad). And of course, the classloader was using the incorrect one. Mapping was correct, it was just not read :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 11:47 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
LOL...

Classloaders cause so many problems! I've always said one classloader that does EVERYTHING, but nobody ever listens.

_________________
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:
PostPosted: Mon Jun 30, 2008 3:55 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Cameron McKenzie wrote:
Classloaders cause so many problems! I've always said one classloader that does EVERYTHING, but nobody ever listens.

Totally unrelated :) I just put 2 jars in different versions in same classloader. Forgot to remove the old one.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 4:16 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
ugg..you have troubles persisting a many-to-many association...me too!!

I really know all the mapping issues and mapped them right..but I cannot find out where the problem lies..Look at my topic "org.hibernate.PropertyAccessException" here in this forum. Please let me know, if you find an solution..it sucks me really, cos I have been searching for three days without success..

good luck


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 10:47 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Sometimes it's so important to just keep it simple. Take a look at this very simple tutorial on mapping many to many relationships with Hibernate and work from there!

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=19mappingmanytomanyrelationships


Image

Code:
package com.examscam.mappings;
import java.util.*;
import javax.persistence.*;

@Entity
public class LeftManyStudent {

  long id;
  String studentName;
  List<RightManyCourse> courses = new Vector();


  @ManyToMany
  @JoinTable(name = "join_table",
joinColumns = { @JoinColumn(name = "lmstudent_id")},
inverseJoinColumns={@JoinColumn(name="rmcourse_id")}   
  )
  public List<RightManyCourse> getCourses(){
    return courses;
  }
  public void setCourses(List<RightManyCourse> righties){
    this.courses = righties;
  }

  @Id
  @GeneratedValue
  public long getId() {return id;}
  public void setId(long id) {this.id = id;}

  public String getStudentName() {
    return studentName;
  }
  public void setStudentName(String s){
    studentName=s;
  }

}


Code:
package com.examscam.mappings;

import java.util.*;
import javax.persistence.*;

@Entity
public class RightManyCourse {
  long id;
  String courseCode;
  List<LeftManyStudent> students = new Vector();

@ManyToMany
@JoinTable(name = "join_table",
joinColumns={@JoinColumn(name="rmcourse_id")},
inverseJoinColumns={@JoinColumn(name="lmstudent_id")})
  public List<LeftManyStudent> getStudents() {
    return students;
  }
  public void setStudents(List<LeftManyStudent> lefties){
    this.students = lefties;
  }

  @Id
  @GeneratedValue
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }

  public String getCourseCode() {
    return courseCode;
  }
  public void setCourseCode(String courseCode) {
    this.courseCode = courseCode;
  }

}



Code:
public static void main (String args[]) {
HibernateUtil.recreateDatabase();

  LeftManyStudent student01 = new LeftManyStudent();
  student01.setStudentName("Jim Jump");
  LeftManyStudent student02 = new LeftManyStudent();
  student02.setStudentName("Julie Camp");
  LeftManyStudent student03 = new LeftManyStudent();
  student03.setStudentName("Cam Johnson");
  LeftManyStudent student04 = new LeftManyStudent();
  student04.setStudentName("Marcus McKenzie");
  RightManyCourse java101 = new RightManyCourse();
  java101.setCourseCode("Java-101");
  RightManyCourse cplus101 = new RightManyCourse();
  cplus101.setCourseCode("C++ - 101");
  RightManyCourse math101 = new RightManyCourse();
  math101.setCourseCode("Math - 101");

  java101.getStudents().add(student01);
  java101.getStudents().add(student02);
  java101.getStudents().add(student03);
  cplus101.getStudents().add(student02);
  cplus101.getStudents().add(student03);
  math101.getStudents().add(student04);

  Session session = HibernateUtil.beginTransaction();
  session.save(student01);
  session.save(student02);
  session.save(student03);
  session.save(student04);
  session.save(java101);
  session.save(cplus101);
  session.save(math101);
  HibernateUtil.commitTransaction();
}



http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=19mappingmanytomanyrelationships



Image

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