-->
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.  [ 9 posts ] 
Author Message
 Post subject: Simple Ordered Many to Many
PostPosted: Sat Jun 26, 2004 4:09 am 
Newbie

Joined: Sat Jun 26, 2004 4:04 am
Posts: 9
Hi, I've been searching in the forum and I've read the documentation but couldn't find the answer to this simple problem.

And ordered many to many on one side, while the other side has no ordering (doesn't care).

So, in code, this would be
Code:
class OneSide {
    List orderedList;
}
class OtherSide {
    Set unorderedSet;
}


in normal sql, I would create an 'in-between' table like this:
Code:
T_Table
    FK_OneSide
    FK_OtherSide
    TAB_Order


How do I create an hibernate mapping for this? Sorry for my newbieness..

Regards,
- Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 10:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
On one side use <bag order-by>, on the other side use <set>.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:12 am 
Newbie

Joined: Sat Jun 26, 2004 4:04 am
Posts: 9
Hi, I've been trying to use the bag/set method, but I've been unable to add the 'element' or 'index' to the bag.

Here is how far I've gotten so far.

Code:
<hibernate-mapping>
    <class name="bagtest.data.OneSide" table="T_OneSide">
        <id name="id" column="OneSideId" type="long">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="ONS_Name"/>
        <bag name="othersides" table="T_Connection" inverse="true" order-by="CON_Order ASC">
            <key column="FK_OneSideId"/>
            <element column="CON_Order" type="integer"/>
            <many-to-many column="FK_OtherSideId" class="bagtest.data.OtherSide"/>
        </bag>
    </class>
    <class name="bagtest.data.OtherSide" table="T_OtherSide">
        <id name="id" column="OtherSideId" type="long">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="OTS_Name"/>
        <set name="onesides" table="T_Connection">
            <key column="FK_OtherSideID"/>
            <many-to-many column="FK_OneSideId" class="bagtest.data.OneSide"/>
        </set>
    </class>
</hibernate-mapping>


I want an 'OtherSide' to have a seperate order in any number of 'OneSides'.

Example:
Code:
OneSide  OtherSide  Order
   1        1         0
   1        2         1
   1        3         2
   2        1         1
   2        2         3
   2        3         0


Any help is greatly appreciated.

Regards,
- Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:14 am 
Newbie

Joined: Sat Jun 26, 2004 4:04 am
Posts: 9
I forget: hibernate chokes on the mapping because element and many-to-many cannot be part of the same 'bag'. and 'index' doesn't exist for 'bag'.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:30 am 
Newbie

Joined: Sat Jun 26, 2004 4:04 am
Posts: 9
"i forgot" = "i forgot to add". I still don't have a solution :) why isn't there an edit button?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 2:36 pm 
Newbie

Joined: Sat Jun 26, 2004 4:04 am
Posts: 9
Code:
<hibernate-mapping>
    <class name="bagtest.data.OneSide" table="T_OneSide">
        <id name="id" column="OneSideId" type="long">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="ONS_Name"/>
        <list name="othersides" table="T_Connection" inverse="true">
            <key column="FK_OneSideId"/>
            <index column="CON_Order" type="integer"/>
            <many-to-many column="FK_OtherSideId" class="bagtest.data.OtherSide"/>
        </list>
    </class>
    <class name="bagtest.data.OtherSide" table="T_OtherSide">
        <id name="id" column="OtherSideId" type="long">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="OTS_Name"/>
        <set name="onesides" table="T_Connection">
            <key column="FK_OtherSideID"/>
            <many-to-many column="FK_OneSideId" class="bagtest.data.OneSide"/>
        </set>
    </class>
</hibernate-mapping>


Ok, I've gotten a little further. With a list, it is supposed to work, but 'order-by' doesn't work on a list. Also, I have to create the "T_Connection" table myself (?). And finally, I can add items the the list, but in the 'connection' table, "NULL" values are inserted (mysql).

Suggestions?

Regards,
- Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:24 am 
Newbie

Joined: Sat Jun 26, 2004 4:04 am
Posts: 9
Ok, it seems it is impossible to have an ordered list on one side. So I proceeded to use a composite element.

Code:
<hibernate-mapping>
    <class name="bagtest.data.OneSide" table="T_OneSide">
        <id name="id" column="OneSideId" type="integer">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="ONS_Name"/>
        <set name="othersides" table="T_Connection">
            <key column="FK_OneSideId"/>
            <composite-element class="bagtest.data.Relationship">
                <property name="order" column="CON_Order" type="integer"/>
                <many-to-one name="otherSide" column="FK_OtherSideId" class="bagtest.data.OtherSide"/>
            </composite-element>
        </set>
    </class>
    <class name="bagtest.data.OtherSide" table="T_OtherSide">
        <id name="id" column="OtherSideId" type="integer">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="OTS_Name"/>
        <set name="onesides" table="T_Connection" inverse="true">
            <key column="FK_OtherSideId"/>
            <many-to-many class="bagtest.data.OneSide" column="FK_OneSideId"/>
        </set>
    </class>
</hibernate-mapping>


This works (although I don't like it... I would rather have a map at the "OneSide" side.) but for some reason hibernate creates duplicate entries in the database the moment i add 'sort="bagtest.RelationOrderComparator"' to the 'othersides' set. When I remove it, it works fine.

CompareTo method in Relationship.java
Code:
public int compareTo(Object arg0) {
    Relationship other = (Relationship) arg0;
    // TODO Auto-generated method stub
    Integer order1Int = new Integer(this.getOrder());
    Integer order2Int = new Integer(other.getOrder());

    return order1Int.compareTo(order2Int);
}


Compare method in RelationOrderComparator
Code:
public int compare(Object arg0, Object arg1) {
    Relationship relationship1 = (Relationship) arg0;
    Relationship relationship2 = (Relationship) arg1;
   
    return relationship1.compareTo(relationship2);
}


Some help from the team would be appreciated!

Regards,
- Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 9:14 am 
Newbie

Joined: Sat Jun 26, 2004 4:04 am
Posts: 9
I sortof solved my own problem I guess... When I said 'i would rather have a map at the oneside end' I figured, why not.

The only thing I'm worried about now: is the ordering guaranteed for a map? like in a TreeMap? I added 'order-by="CON_Order ASC"' and in my small tests it works ok.

Code:
<hibernate-mapping>
    <class name="bagtest.data.OneSide" table="T_OneSide">
        <id name="id" column="OneSideId" type="integer">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="ONS_Name"/>
        <map name="othersides" table="T_Connection" order-by="CON_Order ASC">
            <key column="FK_OneSideId"/>
            <index column="CON_Order" type="integer"/>
            <many-to-many class="bagtest.data.OtherSide" column="FK_OtherSideId"/>
        </map>
    </class>
    <class name="bagtest.data.OtherSide" table="T_OtherSide">
        <id name="id" column="OtherSideId" type="integer">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="OTS_Name"/>
        <set name="onesides" table="T_Connection" inverse="true">
            <key column="FK_OtherSideId"/>
            <many-to-many class="bagtest.data.OneSide" column="FK_OneSideId"/>
        </set>
    </class>
</hibernate-mapping>


I'll post the java code to insert/select/delete later. (have to maintain the order). Comments/improvements are welcome ofcourse.


Top
 Profile  
 
 Post subject: Ordering and Many-to-Many
PostPosted: Fri Jul 09, 2004 10:48 pm 
Newbie

Joined: Fri Jul 09, 2004 10:45 pm
Posts: 1
I'm a beginner too, and I'm trying to sort out how to deal with fields in many-to-many relationships, so thank you for stumbling through this process, it is enlightening.

I'd really like to see how you managed the update code.


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