-->
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: ordering of many-to-many collections/explicit polymorphism
PostPosted: Wed Apr 26, 2006 5:27 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:40 am
Posts: 24
I am using spring with hibernate and have a few problems.

Two of them:

Hibernate version:
3.1
Mapping documents:
Code:
<hibernate-mapping>
    <class name="de.producto.tbw.model.Product" table="produkte" polymorphism = "explicit">
        <id name="productId" column="p_id">
            <generator class="native"/>
        </id>

        <many-to-one name="manufacturer" column="h_id" not-null="true" fetch="join" lazy="false"/>
   
        <bag name="categories" table = "tbt_kat.prod_kat3" inverse="true" lazy="true" order-by="kat_order_id">
            <key column="p_id"/>
            <many-to-many class="de.producto.tbw.model.Cat3" column = "id" />
        </bag>      
      
        <property  name="name" column="name"/>
        <property name="price" column="preis"/>
      
       
        <joined-subclass name = "de.producto.tbw.model.DetailedProduct" table ="tb_cache.op_n_t_per_prod" lazy="true">
            <key column = "p_id"/>
            <property name="productReviewCount" column="test_count"/>   
        </joined-subclass>
    </class>
      
    <query name="detailedProduct" cacheable="true">
        <![CDATA[
            select detailedProduct, detailedProduct.categories, detailedProduct.manufacturer
                from DetailedProduct detailedProduct
                where detailedProduct.productId = :productId
        ]]>
    </query>
</hibernate-mapping>


Name and version of the database you are using:
MySQL 4.1.11

First question:

Code between sessionFactory.openSession() and session.close():
Code:
Query query = getSession().getNamedQuery("detailedProduct");
query.setInteger("productId", productId);
if (query.list().size() > 0) {
    Object[] result = (Object[]) query.list().get(0);
    return (DetailedProduct) result[0];
}


The generated SQL (show_sql=true):
Code:
select detailedpr0_.p_id as p1_0_, product0_.name as name4_1_ ... from tb_cache.op_n_t_per_prod detailedpr0_ inner join produkte detailedpr0_1_ on detailedpr0_.p_id=detailedpr0_1_.p_id inner join tbt_kat.prod_kat3 categories1_ on detailedpr0_.p_id=categories1_.p_id inner join tbt_kat.kat3 cat3x2_ on categories1_.id=cat3x2_.id, hersteller manufactur3_ where detailedpr0_1_.h_id=manufactur3_.h_id and detailedpr0_.p_id=?


How can I order my categories, since the order-by property is ignored in the query?

Second question:

Code between sessionFactory.openSession() and session.close():
Code:
return (Product) getHibernateTemplate().get(Product.class, productId);


The generated SQL (show_sql=true):
Code:
select product0_.p_id as p1_1_, product0_.h_id as h2_4_1_, product0_.name as name4_1_, ... from produkte product0_ left outer join tb_cache.op_n_t_per_prod product0_1_ on product0_.p_id=product0_1_.p_id inner join hersteller manufactur1_ on product0_.h_id=manufactur1_.h_id where product0_.p_id=?


Why does hibernate join the tb_cache.op_n_t_per_prod table, even if I only want to get the product, have decleared polymorphism="explicit" and fetching for the subclass as lazy?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 8:13 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
1)

<![CDATA[
select detailedProduct, detailedProduct.categories, detailedProduct.manufacturer
from DetailedProduct detailedProduct
where detailedProduct.productId = :productId
ORDER BY...
]]>

just add ORDER BY

2)

<![CDATA[
select detailedProduct, detailedProduct.categories, detailedProduct.manufacturer
from DetailedProduct detailedProduct
where detailedProduct.productId = :productId
]]>

problem is you retrieve the object "twice":
detailedProduct, detailedProduct.categories, detailedProduct.manufacturer

--> either specify the columns to be retrieved
detailedProduct.categories, detailedProduct.manufacturer

OR the whole entity
detailedProduct

_________________
Please don't forget to give credit, if my posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 3:58 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:40 am
Posts: 24
axismundi wrote:
1)

<![CDATA[
select detailedProduct, detailedProduct.categories, detailedProduct.manufacturer
from DetailedProduct detailedProduct
where detailedProduct.productId = :productId
ORDER BY...
]]>

just add ORDER BY

Uh, thanks. I just didn't dare to just put the database column name into the ORDER BY, but well... it worked.

axismundi wrote:
2)

<![CDATA[
select detailedProduct, detailedProduct.categories, detailedProduct.manufacturer
from DetailedProduct detailedProduct
where detailedProduct.productId = :productId
]]>

problem is you retrieve the object "twice":
detailedProduct, detailedProduct.categories, detailedProduct.manufacturer

--> either specify the columns to be retrieved
detailedProduct.categories, detailedProduct.manufacturer

OR the whole entity
detailedProduct

Well I don't retreive the product via the query but just by retreiving an instance of the Product class by id. But whenever I want to retreive an instance of the Product class, the thing returns an instance of DetailedProduct


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 5:20 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
Thomson wrote:
axismundi wrote:
1)

<![CDATA[
select detailedProduct, detailedProduct.categories, detailedProduct.manufacturer
from DetailedProduct detailedProduct
where detailedProduct.productId = :productId
ORDER BY...
]]>

just add ORDER BY

Uh, thanks. I just didn't dare to just put the database column name into the ORDER BY, but well... it worked.

axismundi wrote:
2)

<![CDATA[
select detailedProduct, detailedProduct.categories, detailedProduct.manufacturer
from DetailedProduct detailedProduct
where detailedProduct.productId = :productId
]]>

problem is you retrieve the object "twice":
detailedProduct, detailedProduct.categories, detailedProduct.manufacturer

--> either specify the columns to be retrieved
detailedProduct.categories, detailedProduct.manufacturer

OR the whole entity
detailedProduct

Well I don't retreive the product via the query but just by retreiving an instance of the Product class by id. But whenever I want to retreive an instance of the Product class, the thing returns an instance of DetailedProduct


if you want to retrieve Product's you need to tell it hibernate:
simple from DetailedProduct detailedProduct
use from Product Product

_________________
Please don't forget to give credit, if my posting helped to solve your problem.


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.