-->
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.  [ 1 post ] 
Author Message
 Post subject: many-to-many + join table = external sql-query won't work
PostPosted: Tue Dec 09, 2008 6:55 pm 
Newbie

Joined: Sun Dec 07, 2008 1:29 pm
Posts: 6
Hello,

Having the mapping below, I can't manage to have the sql-query producing results. I checked all conditions in isolation and everything else works except for the join table PRODUCT_ORDER_REL_ when I try doing the inner join Product / ProductOrder / Order I get no rows even though loading an Order loads all associated products. The PRODUCT_ORDER_REL_ table does not have any mapping or declaration ... is that a problem?

TIA,
regards

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

<hibernate-mapping package="perfectjpattern.jee.database.datamodel">

<!-- Customer is a strong entity that contains Orders -->
<class name="Customer" table="CUSTOMER_" >
<id name="id" column="ID" >
<generator class="native" />
</id>

<property name="name" column="NAME" update="false" />

<list name="orders" cascade="save-update" >
<key column="CUSTOMER_ID" />
<list-index column="ID" />
<one-to-many class="Order" />
</list>

</class>

<!-- Order is a weak entity that depends on Customer and contains Products -->
<class name="Order" table="ORDER_" >
<id name="id" column="ID">
<generator class="native" />
</id>

<many-to-one name="customer" class="Customer" column="CUSTOMER_ID" />

<set name="products" table="PRODUCT_ORDER_REL_" cascade="save-update, delete"
inverse="true" >
<key column="ORDER_ID" />
<many-to-many class="Product" column="PRODUCT_ID" />
</set>

<property name="date" column="DATE" update="false" />
</class>

<!-- Product is a strong entity and references the orders -->
<class name="Product" table="PRODUCT_" >
<id name="id" column="ID" >
<generator class="native" />
</id>

<property name="name" column="NAME" update="false" />
<property name="listPrice" column="LIST_PRICE" update="true" />

<set name="orders" table="PRODUCT_ORDER_REL_" cascade="save-update, delete" >
<key column="PRODUCT_ID" />
<many-to-many class="Order" column="ORDER_ID" />
</set>
</class>

<!-- Named SQL Query -->
<sql-query name="Customer.findByTotalSpendingWithinPeriod">
<return alias="c1" class="perfectjpattern.jee.database.datamodel.Customer"/>
<![CDATA[SELECT c1.*
FROM CUSTOMER_ c1
WHERE c1.ID IN
(SELECT c2.ID
FROM CUSTOMER_ c2,
ORDER_ o,
PRODUCT_ORDER_REL_ por,
PRODUCT_ p
WHERE o.date BETWEEN :Begin AND :End AND
c2.ID=o.CUSTOMER_ID AND
o.ID=por.ORDER_ID AND
por.PRODUCT_ID=p.ID
GROUP BY c2.ID
HAVING SUM(p.LIST_PRICE) > :Total
)
]]>
</sql-query>

</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.