-->
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.  [ 5 posts ] 
Author Message
 Post subject: Cross table with an attribute (many to many)
PostPosted: Wed Mar 19, 2008 10:05 am 
Newbie

Joined: Thu Feb 14, 2008 5:34 am
Posts: 8
I know how to use Cross Tables in Hibernate, but now I have an extra attribute in my cross table, how can I retrieve that?
The attribute is used to save the order of some tasks.

This is my many-to-many part from my mapping file


Code:
...
  <bag name="File" table="tasks" lazy="false" >
      <key column="FileID"></key>
      <many-to-many class="Plan.Task, Plan" column="TaskID"/>
    </bag>
..


My Cross table looks like this
Code:
FileID (PK)        |    Integer
TaskID (PK)      |    Integer
Order (PK)        |    Integer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 20, 2008 9:37 am 
Newbie

Joined: Thu Feb 14, 2008 5:34 am
Posts: 8
At least someone should have had this kind of problem or am I the only one?


Top
 Profile  
 
 Post subject: Cross table with an attribute (many to many)
PostPosted: Thu Mar 20, 2008 10:24 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I'm not sure if this is the problem, but I think bags are unordered. If you change it to an array, then I think it requires an index column to specify the ordering.

http://www.hibernate.org/hib_docs/nhibernate/html/collections.html#collections-ofvalues

Code:
<array name="File" table="tasks">
    <key column="FileID" />
    <index column="Order" />
    <many-to-many class="Plan.Task, Plan" column="TaskID" />
</array>


Is that what you meant?

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 20, 2008 1:22 pm 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
you mean how to retrieve via code?

one solution would be to map that "cross table" to an object
and to replace the many-to-many association with 2 one-to-many's

by cross table you mean a table for many-to-many associations,
right?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 6:22 am 
Newbie

Joined: Thu Feb 14, 2008 5:34 am
Posts: 8
What FlukeFan said is right for me although I have one small problem left:
it should be possible to add the same tasks, but when using the solution of FlukeFan, I get an exception:

Code:
a different object with the same identifier value was already associated with the session.


When using a bag, I don't get this exception, I've tried to use list in case of array, but that has the same result.
Is there a way to avoid this error?
Edit: solved, I check now if I had already added the object in the list and I just add the same object again, instead of making a new object.


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