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.  [ 3 posts ] 
Author Message
 Post subject: Problem with expressing a simple query in HQL
PostPosted: Fri Aug 08, 2008 9:39 am 
Newbie

Joined: Thu Jul 31, 2008 7:45 am
Posts: 5
Location: Sorunda
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

NHibernate version:1.2.1

I've been struggling for quite a while now with a rather simple query; what I want to do is to write the following SQL query as a HQL query and execute it:
Quote:
SELECT T.OrderID, B.ShipName from Order_Details as T, Orders as B WHERE T.OrderID=B.OrderID


I've Looked int the HQL part of the reference documentation, searched the webb for clues and peeked at the NHibernate test code without getting much wiser. The nearest I've been to success is with:
Quote:
IQuery ODqt = session.CreateQuery("select T.orderID from OrderDetailsEntity as T, select B.shipName from OrdersEntity as B where T.orderID=B.orderID");

But with that one I'll only get shipName back and not both orderID and shipName. I'll be gratefull for any advice.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 08, 2008 10:49 am 
Newbie

Joined: Sat Apr 05, 2008 6:33 pm
Posts: 16
Do you have the relation between the OrderEntity and the OrderDetailsEntity mapped in nhibernate?

If you do you should be able to use this hql query:
"select o.shipName, o.Details.orderID from OrdersEntity"

If the relation is mapped correctly this should result in a correct join and projections. I am not 100% sure you can do this in HQL, I use the critiera API mostly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 3:55 am 
Newbie

Joined: Thu Jul 31, 2008 7:45 am
Posts: 5
Location: Sorunda
Yes they should be mapped correctly.
OrdersEntity.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="NorthWindNHibernate.OrdersEntity, NorthWindNHibernate" table="Orders">

<id name="orderID" column="OrderID" type="System.Int32"><generator class="native"/></id>

<property name="orderDate" column="OrderDate" access="field.pascalcase-underscore" not-null="false" type="System.DateTime" insert="true" update="true"></property>
<property name="requiredDate" column="RequiredDate" access="field.pascalcase-underscore" not-null="false" type="System.DateTime" insert="true" update="true"></property>
<property name="shippedDate" column="ShippedDate" access="field.pascalcase-underscore" not-null="false" type="System.DateTime" insert="true" update="true"></property>
<property name="freight" column="Freight" access="field.pascalcase-underscore" not-null="false" type="System.Decimal" insert="true" update="true"></property>
<property name="shipName" column="ShipName" access="field.pascalcase-underscore" not-null="false" type="System.String" length="40" insert="true" update="true"></property>
<property name="shipAddress" column="ShipAddress" access="field.pascalcase-underscore" not-null="false" type="System.String" length="60" insert="true" update="true"></property>
<property name="shipCity" column="ShipCity" access="field.pascalcase-underscore" not-null="false" type="System.String" length="15" insert="true" update="true"></property>
<property name="shipRegion" column="ShipRegion" access="field.pascalcase-underscore" not-null="false" type="System.String" length="15" insert="true" update="true"></property>
<property name="shipPostalCode" column="ShipPostalCode" access="field.pascalcase-underscore" not-null="false" type="System.String" length="10" insert="true" update="true"></property>
<property name="shipCountry" column="ShipCountry" access="field.pascalcase-underscore" not-null="false" type="System.String" length="15" insert="true" update="true"></property>

<many-to-one name="Customer"  access="field.pascalcase-underscore" not-null="false" outer-join="auto" insert="true" update="true">
   <column name="CustomerID"/>
</many-to-one>
<many-to-one name="Employee"  access="field.pascalcase-underscore" not-null="false" outer-join="auto" insert="true" update="true">
   <column name="EmployeeID"/>
</many-to-one>
<many-to-one name="ShipVia"  access="field.pascalcase-underscore" not-null="false" outer-join="auto" insert="true" update="true">
   <column name="ShipVia"/>
</many-to-one>

</class>
</hibernate-mapping>

OrderDetailsEntity.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="NorthWindNHibernate.OrderDetailsEntity, NorthWindNHibernate" table="[Order Details]">

<composite-id>
<key-property name="orderID" column="OrderID" type="System.Int32"></key-property>
<key-property name="productID" column="ProductID" type="System.Int32"></key-property>
</composite-id>

<property name="unitPrice" column="UnitPrice" access="field.pascalcase-underscore" not-null="true" type="System.Decimal" insert="true" update="true"></property>
<property name="quantity" column="Quantity" access="field.pascalcase-underscore" not-null="true" type="System.Int16" insert="true" update="true"></property>
<property name="discount" column="Discount" access="field.pascalcase-underscore" not-null="true" type="System.Single" insert="true" update="true"></property>
<many-to-one name="Order" column="OrderID" access="field.pascalcase-underscore" not-null="true" outer-join="auto" insert="false" update="false"></many-to-one>
<many-to-one name="Product" column="ProductID" access="field.pascalcase-underscore" not-null="true" outer-join="auto" insert="false" update="false"></many-to-one>

</class>
</hibernate-mapping>

[/code]

I think I got it to work now :) Thanks!


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