[hibernate-users] Simple relationship problems

Robert Klemme shortcutter at googlemail.com
Mon May 19 05:53:58 EDT 2008


Only a quick check, but:

2008/4/24  <boliver at lvlomas.com>:
>
> I have a very simple DB structure that I can't seem to get going here with
> Hibernate.
>
> The relationship between two tables is quite simple.
>
> Table:  Team - contains the following
> pkid - int
> name - varchar
> divisionpkid - int
>
> Table: Division - contains the following:
> pkid - int
> name - varchar
>
> A team can belong to a division, and division can have many teams.  What I
> want is to get all the teams from the database, and have a reference in
> each team to it's division.
>
> My hibernate hbm file looks like this:
>
> <class name="database.HldivisionEntity" table="hldivision"
> catalog="Twisters2008">
>        <id name="pkid">
>            <column name="pkid" sql-type="int" length="11" precision="0"
> not-null="true"/>
>        </id>
>        <property name="name">
>            <column name="name" sql-type="varchar" length="20"
> precision="0" not-null="false"/>
>        </property>
>    </class>
>    <class name="database.TeamEntity" table="team" catalog="Twisters2008">
>        <id name="pkid">
>            <column name="pkid" sql-type="int" length="11" precision="0"
> not-null="true"/>
>        </id>
>        <property name="name">
>            <column name="name" sql-type="varchar" length="20"
> precision="0" not-null="false"/>
>        </property>
>        <property name="hldivisionpkid">
>            <column name="hldivisionpkid" sql-type="int" length="11"
> precision="0" not-null="false"/>
>        </property>

>From memory: I believe this should not be a regular property but
contained in the relationship definition. Also, you do not want to
define the id as property but a member of type Division.  Please see
the docs for more detail.

>        <one-to-one name="division" class="database.HldivisionEntity"/>

This needs to be many-to-one IIRC.

>    </class>
>
> In my code, when I execute this:
>
> List teamList = session.createQuery("from TeamEntity" ).list();
>
> I get a List returned of the two team records in my Team table, and the
> hldivisionpkid fields are set correctly, the the division object is null.
> So I must be doing something wrong here.  Have I missed something in my
> configuration to make this relationship work correctly?

Btw, I'd rather use annotations if you can in your environment.

Kind regards

robert



More information about the hibernate-users mailing list