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.  [ 2 posts ] 
Author Message
 Post subject: querying on a value in a many-to-one class
PostPosted: Thu Nov 29, 2007 3:18 pm 
Newbie

Joined: Wed Nov 28, 2007 5:37 pm
Posts: 1
Location: Baltimore, MD
I am very to to NHibernate and I am having trouble executing a query with criteria in a mapped many-to-one mapped class.

Here's the hbm xml the order class (I removed many of the properties for brevity sake):

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Orders.Core" assembly="Orders.Core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2" schema="ServiceNetData.dbo" >
<class name="Order" lazy="false" table="ps_order">
<id name="OrderId" column="order_id" unsaved-value="0">
<generator class="native" />
</id>
<property name="OrderStatusId" column="order_status_id" />
<property name="CompanyId" column="company_uid" />
<property name="UserId" column="UserId" />
<many-to-one name="PaymentInformation" class="Payment" lazy="false" column="payment_id" cascade="all" insert="true" update="true" />
<many-to-one name="ServiceCenterInformation" fetch="join" lazy="false" class="ServiceCenter" column="service_center_id" foreign-key="account_id" cascade="none" />
</class>
</hibernate-mapping>


The many-to-one relationship I am interested in is the one named ServiceCenterInformation. This is mapped as follows:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Orders.Core" namespace="Orders.Core" schema="CMDB.dbo" >
<class name="ServiceCenter" lazy="false" table="ps_account">
<id name="AccountId" column="account_id" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="native" />
</id>
<property name="CompanyName" column="company_name" />
<property name="AccountTypeId" column="AccountTypeId" />
<property name="AccountNumber" column="accountNumber" />
<property name="ServiceCenterNumber" column="serviceCenterNumber" />
<property name="Address1" column="address1" />
<property name="City" column="city" />
<property name="State" column="state" />
<property name="Zip" column="zip" />
</class>
</hibernate-mapping>


I am trying to query using a property in many-to-one ServiceCenterInformation class (ServiceCenterNumber). My Order class has a public property for the ServiceCenterInformation Mapping, I was hoping to use a criteria Expression Eq property using the ServiceCenterNumber property within the ServiceCenterInfomation class as seen below.


public IList<Core.Order> GetOrdersForServiceCenter(string serviceCenterNumber, int pageNumber, ref int totalPages)
{
ICriteria crit = NHibernateSession.CreateCriteria(typeof(Core.Order));
crit.Add(Expression.Eq("ServiceCenterInformation.ServiceCenterNumber", serviceCenterNumber.ToString()));
crit.AddOrder(Order.Desc("OrderId"));

return Execute(crit);
}


When I run this, I receive the error "could not resolve property: ServiceCenterInformation.ServiceCenterNumber of: Orders.Core.Order"

Am I going about this the wrong way? Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 5:16 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Change the first two lines of the method to

Code:
ICriteria crit = NHibernateSession.CreateCriteria(typeof(Core.Order)).CreateAlias("ServiceCenterInformation", "s");
crit.Add(Expression.Eq("s.ServiceCenterNumber", serviceCenterNumber.ToString()));


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