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?