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.  [ 2 posts ] 
Author Message
 Post subject: Need help with many-to-many
PostPosted: Tue May 09, 2006 6:36 pm 
Newbie

Joined: Sun May 07, 2006 7:40 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.x

Mapping documents:

Equipment.hbm.xml

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 05.05.2006 11:23:58 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
    <class name="database.EquipmentData" table="equipment" catalog="busmaster">
        <comment></comment>
        <id name="equipmentId" type="int">
            <column name="equipment_id" />
            <generator class="native" />
        </id>
        <property name="internalId" type="string">
            <column name="internal_id" length="20" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="name" type="string">
            <column name="name" length="100" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="requiredseats" type="short">
            <column name="requiredseats" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="requiredstandings" type="short">
            <column name="requiredstandings" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="deleted" type="boolean">
            <column name="deleted" not-null="true">
                <comment></comment>
            </column>
        </property>
    </class>
</hibernate-mapping>


Bus.hbm.xml:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 05.05.2006 11:23:58 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
    <class name="database.BusData" table="bus" catalog="busmaster">
        <comment></comment>
        <id name="busId" type="int">
            <column name="bus_id" />
            <generator class="native" />
        </id>
        <property name="internalId" type="string">
            <column name="internal_id" length="20" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="seats" type="short">
            <column name="seats" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="standingrooms" type="short">
            <column name="standingrooms" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="licenceplate" type="string">
            <column name="licenceplate" length="20">
                <comment></comment>
            </column>
        </property>
        <property name="technical" type="string">
            <column name="technical" length="65535">
                <comment></comment>
            </column>
        </property>
        <many-to-one name="category" class="database.BuscategoryData" not-null="true"/>
        <!--
        <property name="buscategoryId" type="int">
            <column name="buscategory_id" not-null="true">
                <comment></comment>
            </column>
        </property>
        -->
        <property name="internalbus" type="boolean">
            <column name="internalbus" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="deleted" type="boolean">
            <column name="deleted" not-null="true">
                <comment></comment>
            </column>
        </property>
        <!-- here we map our list -->
        <list name="equipment" table="useableequipment">
...........................
           <many-to-many class="database.EquipmentData"/>
        </list>
    </class>
</hibernate-mapping>


DB: http://x-factor.dyndns.org/~austriancod ... 0agent.png

As you can see, i want to have a List in Busclass, so that i can do this:

Code:
Bus b = new Bus();
Equipment e = new Equipment();
e.set...

Equipment e1 = new Equipment();
e1.set...

List useabelequipment = new List();
useabelequipment.add(e);
useabelequipment.add(1);

b.setequipment(useabelequipment);


Can sombody help me to finish my mapping? I need to store the bus_id and the equipment_id in the table useabelequipment

Thanks for any help,
Christian[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 6:52 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I think that you probably want a set or bag, not a list. A list has a list index, so unless you're planning on having a list index column in your mapping table, you can't use a list. (A list index column would be something to the effect of a column with an ordinal in it.. a "position index")

The mapping for a many-to-many collection is usually done like this:
Code:
<set name="Equipemnt" table="BusEquipmentLinkTable">
  <key column="ColumnInBusTableThatLinksToLinkTable" not-null="true"/>
  <many-to-many class="Equipment" column="ColumnInLinkTableThatLinksToEquipmentTable"/>
</set>
If the link from the Equipment table to the link table isn't via Equipment's id column, then you can put a property-ref="NameOfPropertyInEquipmentWhoseColumnMapsToLinkTable" in the many-to-many element.

The field/property in the java class can be either Collection or Set type. If you're committed to using List type, you can replace <set> with <bag>, but there are some performance penalties if you do that.

_________________
Code tags are your friend. Know them and use them.


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