-->
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.  [ 6 posts ] 
Author Message
 Post subject: One-To-Many Mapping - 3 Tables
PostPosted: Sat Nov 08, 2008 6:44 pm 
Newbie

Joined: Thu Oct 30, 2008 8:04 am
Posts: 5
Hi, how best should i do that?

I have 3 Tables.

Item, CompItem and Price. One Item can have many Prices, One Item can have many CompItems and one CompItem can have many Prices.

so, i want to have a method with the parameter ItemId. And i want to get back all Prices of that Item and also all Prices of the corresponding CompItems.

So, how best should i do that? I mean, i can do it with a join and several loops. but is this the best approach?

best regards, Philipp


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 4:17 am 
Beginner
Beginner

Joined: Tue Oct 28, 2008 6:41 pm
Posts: 20
Hi,
I'm new to hibernate but it seems the best approach is to create your mappings as below and a hierarchy of loops like you said . Looping item.compitem.price inside each item.compitem inside each item in the outerloop . In the outer loop you get item.price for the non compitems.

In Item.hbm.xml

map a one to many relationship to Prices
map a second 1 to many relationship to comp items

Item class has a price property
and a compitems property

In CompItem.hbm.xml map a 1 to many relationship to prices

hope this helps.


Top
 Profile  
 
 Post subject: limiting teh number of sql queries created by hibernate
PostPosted: Mon Nov 10, 2008 4:18 am 
Beginner
Beginner

Joined: Tue Oct 28, 2008 6:41 pm
Posts: 20
Hibernate seems to run a separate query for each child object on an obect that you map . I understand the solution to this is including a series of left joins in your hibernate query but I get the following exception when i do this.

org.hibernate.QueryException: illegal attempt to dereference collection

So ,if anybody has a solution to this, that would be great.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 8:17 am 
Newbie

Joined: Thu Oct 30, 2008 8:04 am
Posts: 5
Well, i just post my mapping files!

For Item=Artikel it is like that:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
        name="org.soa.preismonitor.hibernate.Artikel"
        table="artikel">

        <id
            name="id"
            column="id">
            <generator class="native"/>
        </id>

        <property
            name="ItemCode"
            column="ItemCode"/>

        <property
            name="ItemName"
            column="ItemName"/>

        <property
            name="Description"
            column="Description"/>
           
        <joined-subclass name="org.soa.preismonitor.hibernate.FremdArtikel"
      table="fremdartikel">
      <key column="ID"/>
      <many-to-one name="RefItem" class="org.soa.preismonitor.hibernate.Artikel" column="refitem"/>
      <many-to-one name="CompCode" class="org.soa.preismonitor.hibernate.Mitbewerb" column="compcode"/>
   </joined-subclass>
       

    </class>

</hibernate-mapping>


and for the Price=Preis it is like that:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
        name="org.soa.preismonitor.hibernate.Preis"
        table="preis">

        <id
            name="id"
            column="id">
            <generator class="native"/>
        </id>

        <many-to-one name="artikel" class="org.soa.preismonitor.hibernate.Artikel" column="refitem"/>

        <property
            name="Datum"
            column="PriceDate"/>
       
        <property
            name="Memo"
            column="Memo"/>     
    </class>

</hibernate-mapping>


Where should i use a collection?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 9:40 am 
Newbie

Joined: Thu Oct 30, 2008 8:04 am
Posts: 5
the problem must bei with the set:

Code:
<set name="preise" table="preis" lazy="true">
            <key column="itemid"/>
            <one-to-many class="org.soa.preismonitor.hibernate.Preis"/>
        </set>


when i add this part of code, i get a parse error of the mapping file!

what is wrong with it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 4:54 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2008 6:41 pm
Posts: 20
Instead of using joined subclass you might try using "bag" in the item.hbm file for price this maps to a List property in the parent class

So you have an item that has a bag of comp items
and a compitem has a bag of prices
and for items without comp items but a number of prices
you have an item that has a bag of prices

Don't worry about mapping the other direction until you have the direction of parent to children working .

in item.hbm.xml :

<bag name="compcode" table="compcode_table" inverse="true"
cascade="all-delete-orphan">
<key column="Item_ID" />
<one-to-many class="compcode" />
</bag>

<bag name="price" table="Price" inverse="true"
cascade= "all-delete-orphan">
<key column="Item_ID" />
<one-to-many class="Price />
</bag>

in compcode.hbm.xml :

<bag name="price" table="price" inverse="true"
cascade="all-delete-orphan">
<key column="compcode_ID" />
<one-to-many class="Price" />
</bag>

in Compcode database table -create an Item_id field - this matches the id in Item


in Price database table - create a compcode_id field - this matches the id in Compcode

in Price database table - create an item_id fleld - this matches the id in Item

in Item class create a List property for compcodes

in Item class create a List property for prices

in item class create List property for price


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