-->
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: many to many relationship problem! Please help me.
PostPosted: Wed Feb 22, 2006 12:20 am 
Newbie

Joined: Tue Feb 21, 2006 11:52 pm
Posts: 2
Hi every body! I'm a newbie in Hibernate and I have a problem in many-many relation. Please help me!
I have 3 file config like this:

Code:
<hibernate-mapping package="com.ise.dbmapping">
    <class name="Dokho" table="dokho">
        <id name="iddokho" column="idDoKho" type="java.lang.String">
            <generator class="assigned"/>
        </id>
        <property name="tendokho" column="tenDoKho"                type="java.lang.String" />
        <property name="mota" column="moTa" type="java.lang.String" />
   <set name="kynang"
            table="kynang_dokho"
            lazy = "false"
            inverse="false"
            sort = "unsorted"
          >
          <key column="iddokho"/>
          <many-to-many class="Kynang"
               column="idKynang"
                      outer-join="auto"/>
        </set>
    </class>
</hibernate-mapping>


Code:
<hibernate-mapping package="com.ise.dbmapping">
    <class name="Kynang" table="kynang">
        <id name="idkynang" column="idKyNang" type="java.lang.String">
            <generator class="assigned"/>
        </id>
        <property name="tenkynang" column="tenKyNang" type="java.lang.String" />
        <property name="soluongcauhoi" column="soLuongCauHoi" type="java.lang.Integer" />

<class="Phanthi"  not-null="true" />
        <set name="dokho"
            table="kynang_dokho"
            lazy = "false"
            inverse="false"
            sort = "unsorted">
          <key column="idkynang"/>
          <many-to-many class="Dokho"
            column="idDokho"
             outer-join="auto"/>
        </set> 
</class>
</hibernate-mapping>


Code:
<hibernate-mapping package="com.ise.dbmapping">
    <class name="KynangDokho" table="kynang_dokho">
        <composite-id name="id" class="KynangDokhoKey">
            <key-many-to-one name="dethi" column="iddethi" class="Dethi"/>
            <key-many-to-one name="dokho" column="iddokho" class="Dokho"/>
            <key-many-to-one name="kynang" column="idkynang" class="Kynang"/>
        </composite-id>

        <property name="soluongcauhoi" column="soluongcauhoi" type="java.lang.Short" />
    </class>
   
</hibernate-mapping>


When I try to get property "soluongcauhoi" from the link table (kynangdokho), it always return "soluongcauhoi" from table "kynang" (or sum of all "soluongcauhoi" in table "kynangdokho" - I'm not sure because my logic were that). I can't get value of property "soluongcauhoi" from kynangdokho.
Two column "soluongcauhoi" in table kynang and kynangdokho have a differrent meaning in my project.
Please show me where I was wrong and how can I fix it?
Best regards!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 7:49 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That mapping implements this:

Dokho (many) -> (many) Kynang (unidirectional)
Phanthi (many) -> (many) Dokho (unidirectional)

KynangDokho isn't related to anything.

Get rid of your KynangDokho mapping, and change your kynang set in your Dokho mapping to be a composite-element class="KynangDokho", with a many-to-many element and a property element in it, for the Dokho and soluongcauhhoi elements, respectively.

If you can't get the to work, post your new errors here.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 12:08 am 
Newbie

Joined: Tue Feb 21, 2006 11:52 pm
Posts: 2
tenwit wrote:
That mapping implements this:

Dokho (many) -> (many) Kynang (unidirectional)
Phanthi (many) -> (many) Dokho (unidirectional)

KynangDokho isn't related to anything.

Get rid of your KynangDokho mapping, and change your kynang set in your Dokho mapping to be a composite-element class="KynangDokho", with a many-to-many element and a property element in it, for the Dokho and soluongcauhhoi elements, respectively.

If you can't get the to work, post your new errors here.


You right! But if I use Dokho(many) -> (many) Kynang (unidirectional) as your suggest, I can't retrieve data from link table (KynangDokho). I need to select property "soluongcauhoi" in table "KynangDokho" because it different from "soluongcauhoi" in table "Kynang"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 12:38 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can. In the sets implementing the many-to-many, use a composite-element. One part will be the special soluongcauhhoi property, and one part will be the many-to-many. Use class="KynangDokho" so that the correct class is instantiate by Hibernate.

You can do this in both directions to make the relationship bidirectional, if you like.

Examples of this are in the ref docs. For the rel3.1 ref docs, section 8.2 describes composite-element with many-to-one. You can extrapolate to use it with many-to-many. Actually, I'm not sure if using it with many-to-many is very useful: you might find that you're better off using many-to-one from the sets to the KynangDokho class (in each direction), and one-to-many from the KynangDokho class in both directions. This achieves the same thing, and you won't have to use composite-element. It's also eaasier to maintain (in my opinion).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 8:01 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
could you please post your solutions??

thanks angela


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.