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.  [ 4 posts ] 
Author Message
 Post subject: Need help with one to one mapping
PostPosted: Sun May 07, 2006 8:04 pm 
Newbie

Joined: Sun May 07, 2006 7:40 pm
Posts: 3
Hibernate version:
3.x

Mapping documents:

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>
        <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>
    </class>
</hibernate-mapping>


Buscategory.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.BuscategoryData" table="buscategory" catalog="busmaster">
        <comment></comment>
        <id name="buscategoryId" type="int">
            <column name="buscategory_id" />
            <generator class="native" />
        </id>
        <property name="categoryname" type="string">
            <column name="categoryname" length="100" not-null="true">
                <comment></comment>
            </column>
        </property>
    </class>
</hibernate-mapping>


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

As you can see on the picture, the table bus has a foreign key to buscategory. It sounds maybe silly, but at the moment i have no idea how to add this to the mapping files :(
I have read the docs, but i can't under stand it.

Thanks,
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 12:57 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's a many to one mapping, not one to one. Two different buses might have the same buscategory. Many buses to one category.

Add this to your bus mapping:
Code:
<many-to-one name="Category" class="database.BuscategoryData" not-null="true"/>
I'm assuming that every bus has one and only one category. Remove the not-null if bues may have null categories (i.e. zero or one categories).

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 12:27 pm 
Newbie

Joined: Sun May 07, 2006 7:40 pm
Posts: 3
Thanks.. works very well :)


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

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You're welcome. Don't forget to rate.

_________________
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.  [ 4 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.