-->
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: No entry in join table using many-to-many
PostPosted: Mon Aug 22, 2005 9:32 am 
Newbie

Joined: Tue Jul 26, 2005 6:26 am
Posts: 15
Hi. I'm having some trouble with an unidirectional many-to-many mapping using a join table. After reducing to the simplest possible case the problem remains, so I guess I got something wrong concerning these mappings.

As mentioned below, I have two classes, A and B. A contains several associations on B, this should happen in a seperate join table. I adapted the example from the manual (at least I think so), but no entry in the join table is made.

My code is just the following:

Code:
public static void main(String[] args) {
  A a = new A();
  a.addChild(new B());
  HibernateUtil.getSession().save(a);
}


If I execute this, a and b get inserted, but no entry in the join table is made.

So how is this supposed to work?

Thanks in advance.


Hibernate version: 3.0

Mapping documents:

A.hbm.xml::
Code:
<hibernate-mapping>
<class name="de.bebit.blackbook.test.A" table="test_A">
  <id name="id">
   <generator class="native"/>
  </id>
  <set name="children" table="test_A_B" cascade="all">
   <key column="a_id"/>
   <many-to-many column="b_id" class="de.bebit.blackbook.test.B"/>
  </set>
</class>
</hibernate-mapping>


B.hbm.xml:
Code:
<hibernate-mapping>
<class name="de.bebit.blackbook.test.B" table="test_B">
  <id name="id">
   <generator class="native"/>
  </id>
</class>
</hibernate-mapping>


Name and version of the database you are using: MySQL 4.1


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 10:01 am 
Newbie

Joined: Tue Aug 02, 2005 1:40 pm
Posts: 16
Location: US
Hello Martin;

I had just come out of such set of problems with one-to-one and one-to-many. You need to make the mapping bi-directional.

In your add method for class A should be

bSist would be any kind of collection that you are using to persist the b objects

addChild(B b){
b.setA(this);
if(bSet==null){
bSet = new HashSet();
}
bList.add(b);
}

change your a mapping to look something like this: added inverse= "true"

<hibernate-mapping>
<class name="de.bebit.blackbook.test.A" table="test_A">
<id name="id">
<generator class="native"/>
</id>
<set name="children" table="test_A_B" cascade="all" inverse="true">
<key column="a_id"/>
<many-to-many column="b_id" class="de.bebit.blackbook.test.B"/>
</set>
</class>
</hibernate-mapping>



also try setting the unsaved-value="any" for in mapping of B such as
add a setter and getter for "a"


<hibernate-mapping>
<class name="de.bebit.blackbook.test.B" table="test_B">
<id name="id" unsaved-value="any">
<generator class="native"/>
</id>
<many-to-many cascade="none" name ="a" column="b_id" insert ="false" update = "false" class="de.bebit.blackbook.test.A"/>
</class>
</hibernate-mapping>

for reference you can look at an example for many-to-many given in this tutorial. I belive doing all these should solve your problem


http://www.warfrog.com/hibernatetutorial2/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 3:19 am 
Newbie

Joined: Tue Jul 26, 2005 6:26 am
Posts: 15
Thanks for your help, but it didn't help me :(

But finally, I got the error. I didn't use transactions in my example, and either did I flush the session, I thought saving the objects would be enough, but it wasn't!

Now I flush the session afterwards, and the association is made. Whether I use uni- or bidirectional association btw doesn't change anything, at least not in this case.

So, problem solved.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.