-->
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: Many-to-many association problem
PostPosted: Mon Jan 05, 2004 11:04 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
Hi,

I have a many-to-many association (Category<->Items). If I look for a Category by Id I get a correct filled Category instance returned with all
available Items in this Category, on the other hand if I search for an item I get the correct filled Item instance with all associated categories back.
Therefore I guess my mapping is correct.

The problem I encounter is, when I look for a Category and Items which only match a certain description I get the correct number of rows back in the List, but the List objects are not Item objects but java.lang.Objects.

Here is my lookup method.

Code:
    public List getCategoryItemsByDescr( long catId, String descr )
    {
        return getHibernateTemplate().find( "from Category cat, Item item where cat.id = ? and item.name like ?", new Object[] { new Long( catId ), "%" + descr + "%" } );
    }


I checked the debug log messages and executed the generated sql, the result was correct.

Why are my objects in the List not Item instances?

I think my mapping is correct, but I cannot figure out what I am doing wrong.

Any help greatly appreciated.

Thanks,

juergen

P.S.: Just in case, this is my mapping:

Code:
   <class name="com.openshop.pizzashop.domain.Category" table="category">
        <id name="id" type="long" unsaved-value="-1">
            <column name="id" sql-type="number" not-null="true"/>
            <generator class="identity"/>
        </id>
        <property name="descr">
            <column name="descr" sql-type="varchar(30)"/>
        </property>
        <set name="items" table="link">
            <key>
                <column name="catno" not-null="true"/>
            </key>
            <many-to-many class="com.openshop.pizzashop.domain.Item">
                <column name="artno" not-null="true"/>
            </many-to-many>
        </set>

    </class>

    <class name="com.openshop.pizzashop.domain.Item" table="items">
        <id name="id" type="long" unsaved-value="-1">
            <column name="itemid" sql-type="number" not-null="true"/>
            <generator class="identity"/>
        </id>

        <property name="name">
            <column name="product" sql-type="varchar(50)"/>
        </property>
        <property name="descr">
            <column name="description" sql-type="varchar(255)"/>
        </property>
        <property name="price">
            <column name="price" sql-type="varchar(20)"/>
        </property>
        <property name="tax">
            <column name="tax" sql-type="number"/>
        </property>
        <property name="picture">
            <column name="picture" sql-type="varchar(100)"/>
        </property>
        <property name="cmsId">
            <column name="cms_id" sql-type="varchar(50)"/>
        </property>
        <set name="catIds" table="link">
            <key>
                <column name="artno" not-null="true"/>
            </key>
            <many-to-many class="com.openshop.pizzashop.domain.Category">
                <column name="catno" not-null="true"/>
            </many-to-many>
        </set>

    </class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 1:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you do

Code:
from Category cat, Item item
,
you will get as Result a List of Object[2] where every entry contains the Category in the first array entry, and the item in the second. If you want just the items, use:

Code:
select item from Category cat, Item item

However, your query is wrong anyways. It should be something like this IMHO:

Code:
select item from Category cat left join cat.items item where cat.id = ? and item.name like ?


Top
 Profile  
 
 Post subject: Thanks for your help, will try this.
PostPosted: Tue Jan 06, 2004 10:45 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
juergen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 11:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
michael


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.