-->
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.  [ 11 posts ] 
Author Message
 Post subject: how to retrieval object from <any> mapping
PostPosted: Fri Sep 12, 2003 4:01 am 
Newbie

Joined: Wed Sep 10, 2003 11:41 pm
Posts: 7
Hi:
I store different products to one order table, I use <any>mapping.
ProductA and ProductB implements an interface 'Product' which define common setter/getter methods.
I also write test.ProductTypeMapper.java, (followed by the instruction in 122.html)
Code:
    static public final Integer PRODUCT_A = new Integer(1);
    static public final Integer PRODUCT_B = new Integer(2);


Add different Products to Order works fine, the problem is when I try to get orders from a ProductA object like this:
Code:
ProductA ap = (ProductA) sess.load(ProductA.class, new Long(1));
Set orders = ap.getOrders();


It also returns ProductB which productId=1! I do only need ProductA.
Hibernate output sql is:
Code:
select ...  from Orders orders0_ where orders0_.product_id=?

Why Hibernate did NOT generate 'where orders0_.product_id=? and orders0_.product_type=PRODUCT_A'?

And how can I get all ProductA or ProductB orders from Order ?
(I mean: in SQL: select * from Order where product_type=PRODUCT_A)


Following is Order and other hbm:
Order.hbm.xml:
Code:
    <class name="test.Order" table="Orders">
        <id name="orderId" type="int" unsaved-value="null" >
            <column name="order_id" sql-type="int" not-null="true"/>
            <generator class="native"/>
        </id>
        <any name="product" id-type="int" meta-type="test.ProductTypeMapper">
           <column name="product_type" />
           <column name="product_id" />
        </any>
        <property name="client" column="client" type="string" />
        <property name="orderTime" column="order_time" type="date" />
    </class>


ProductA.hbm.xml
Code:
    <class name="test.ProductA" table="product_A">
        <id name="productId" type="int" unsaved-value="null" >
            <column name="product_id" sql-type="int" not-null="true"/>
            <generator class="native"/>
        </id>
        <property name="type" type="string" />
        <set name="orders" lazy="true" inverse="true">
            <key column="product_id" />
            <one-to-many class="test.Order"/>
        </set>
    </class


ProductB.hbm.xml
Code:
    <class name="test.ProductA" table="product_A">
        <id name="productId" type="int" unsaved-value="null" >
            <column name="product_id" sql-type="int" not-null="true"/>
            <generator class="native"/>
        </id>
        <property name="type" type="string" />
        <set name="orders" lazy="true" inverse="true">
            <key column="product_id" />
            <one-to-many class="test.Order"/>
        </set>
    </class



Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 4:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Add a where attribute to the collection mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 5:19 am 
Newbie

Joined: Wed Sep 10, 2003 11:41 pm
Posts: 7
thanks gavin!

I've add 'where product_type=1' in ProductA.hbm.xml and 'where product_type=2' in ProductB.hbm.xml
now "Set orders = ap.getOrders(); " OK.

But how can I get all ProductA orders from Order ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 5:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
How can you what?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 5:24 am 
Newbie

Joined: Wed Sep 10, 2003 11:41 pm
Posts: 7
I mean how to write HQL to get result like SQL "select * from Order where product_type=1"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 5:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
from Order o where p.product.class = :productMetaIdValue


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 5:31 am 
Newbie

Joined: Wed Sep 10, 2003 11:41 pm
Posts: 7
Thanks gavin!!!!!!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 6:00 am 
Newbie

Joined: Wed Sep 10, 2003 11:41 pm
Posts: 7
a HQL question:
how can I write HQL to do a SQL like :
"select o.* from Order o left join ProductA a using(product_id) where a.type=xxx" ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 6:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
what is using()? doesn't look like ANSI SQL.

And why would you use a column of a left joined table in the where clause? That makes it an inner join.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 6:41 am 
Newbie

Joined: Wed Sep 10, 2003 11:41 pm
Posts: 7
I want select a list of productA from Order with Product type is xxx
I tried
"select o.product from Order o where o.product.class=1 and o.product.type='xxx'"
hibernate report error:
Code:
2 errors occurred while listing (and calling getPathNames).
could not resolve property type: product.width [select o.product from test.Order o where o.product.type='xxx']
could not resolve property type: product.width [select o.product from test.Order o where o.product.type='xxx']

Is that because
"select o.product from Order o where o.product.class=1" only returns object[] not Product[] ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 6:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Funny looking message. Bu anyway, no ... this will not work. Use:


Code:
select prod
from Order ord,
   ProductA prod
where ord.product.class='...'
   and ord.product.id = prod.id


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