-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to build the HQL statement for this
PostPosted: Wed Mar 29, 2006 11:26 am 
Newbie

Joined: Thu Mar 23, 2006 1:42 pm
Posts: 4
I am trying to write an HQL statement with a subquery. However, when I attempt to use an alias, I get a "could not resolve property" error. Could someone please take a look and see what I'm missing? Thanks.

The error I'm getting is this:

{"could not resolve property:EmployeeID of :PerformanceManagement.ProductionLaborTransaction [from PerformanceManagement.ProductionLaborTransaction plt where plt.EmployeeID = 244 and plt.Day = '3/28/2006 12:00:00 AM' and plt.TransactionID in(select max(plt2.TransactionID) from PerformanceManagement.ProductionLaborTransaction plt2 where plt2.EmployeeID = 244 and plt2.Day = '3/28/2006 12:00:00 AM' group by plt2.ProductionTaskID, plt2.ProductID)]" }

Here is the query I'm trying to build:

IList list = session.Find(string.Format( "from ProductionLaborTransaction plt where plt.EmployeeID = {0} and plt.Day = '{1}' and plt.TransactionID in" +
"(select max(plt2.TransactionID) from ProductionLaborTransaction plt2 where " +
"plt2.EmployeeID = {2} and plt2.Day = '{3}' group by plt2.ProductionTaskID, plt2.ProductID)",
employeeID, day.ToString(), employeeID, day.ToString())
);

Here is the mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="PerformanceManagement.ProductionLaborTransaction, PerformanceManagement" table="ProductionLabor">
<id name="id" access="field" column="TransactionID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>

<many-to-one
name="Employee"
access="nosetter.camelcase"
column="EmployeeID"
class="PerformanceManagement.Employee, PerformanceManagement"
cascade="none"
not-null="true" />

<property name="Day" access="nosetter.camelcase" column="`Day`" type="DateTime" />

<many-to-one
name="Task"
access="nosetter.camelcase"
column="ProductionTaskID"
class="PerformanceManagement.ProductionTask, PerformanceManagement"
cascade="none"
not-null="true" />

<many-to-one
name="Product"
access="nosetter.camelcase"
column="ProductID"
class="PerformanceManagement.Product, PerformanceManagement"
cascade="none"
not-null="true" />


<property name="TransactionDate" access="nosetter.camelcase" column="TransactionDate" type="DateTime" />
<property name="Submitter" access="nosetter.camelcase" column="Submitter" type="String" length="50" />
<property name="Minutes" access="nosetter.camelcase" column="Minutes" type="Int32" />
<property name="Units" access="nosetter.camelcase" column="Units" type="Int32" />
<property name="Comments" access="nosetter.camelcase" column="Comments" type="String" length="1024" />

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:28 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
What does the ProductionLaborTransaction class look like? Perhaps the property is spelled differently or something.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:38 pm 
Newbie

Joined: Thu Mar 23, 2006 1:42 pm
Posts: 4
Here's the class:
Code:
public class ProductionLaborTransaction : IEntity
   {
      private int id = EntityHelper.UnsavedIdentityValue;
      private Employee employee;
      private DateTime day;
      private ProductionTask task;
      private Product product;
      private DateTime transactionDate;
      private string submitter;
      private int minutes;
      private int units;
      private string comments;

      
      #region IEntity Members

      public int Id
      {
         get
         {
            return this.id;
         }
      }

      #endregion
      public Employee Employee
      {
         get { return employee; }
      }

      public ProductionTask Task
      {
         get { return task; }
      }

      public Product Product
      {
         get { return product; }
      }

      public DateTime Day
      {
         get { return day; }
      }

      public DateTime TransactionDate
      {
         get { return transactionDate; }
      }

      public string Submitter
      {
         get { return this.submitter; }
      }

      public int Minutes
      {
         get { return this.minutes; }
      }

      public int Units
      {
         get{return this.units;}
      }

      public string Comments
      {
         get { return comments; }
      }

      public ProductionLaborTransaction CreateOffset(DateTime transactionDate)
      {
         return new ProductionLaborTransaction(
            this.employee, this.day, this.task, this.product, DateTime.Now, string.Empty,
            this.minutes * -1, this.units * -1, string.Empty);
      }


      public ProductionLaborTransaction(
         Employee employee,
         DateTime day,
         ProductionTask task,
         Product product,
         DateTime transactionDate,
         string submitter,
         int minutes,
         int units,
         string comments)
      {
         Validator.ValidateParameter(employee, "employee");
         Validator.ValidateParameter(task, "task");
         Validator.ValidateParameter(product, "product");
         Validator.ValidateParameter(comments, "comments", true);

         this.employee = employee;
         this.day = day;
         this.task = task;
         this.product = product;
         this.transactionDate = transactionDate;
         this.submitter = submitter;
         this.minutes = minutes;
         this.units = units;
         this.comments = comments;
      }

      private ProductionLaborTransaction()
      {
      }
      
   }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 2:58 pm 
Newbie

Joined: Fri Jan 20, 2006 12:47 pm
Posts: 16
Location: Ottawa, On
HQL is an object oriented query langugage, so you need to reference the property name (i.e Employee) not the database column name(EmployeeID) in your query.

Since Employee is an object, you actually want to do something like where plt.Employee.Id = {0} ... etc where Id is the property name of the Identifier property of the Employee class.

You can also do an inner join on the Employee object

from PerformanceManagement.ProductionLaborTransaction plt inner join plt.Employee as emp where emp.Id = {0}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 3:19 pm 
Newbie

Joined: Thu Mar 23, 2006 1:42 pm
Posts: 4
I have tried using "plt.Employee.Id" and that gives me the same error.

I have also tried running each query by itself without the aliases and everything is fine. I can reference the EmployeeID column with no problem. It is when I use the aliases that it falls apart.

This works fine:
Code:
IList list = session.Find(string.Format("from ProductionLaborTransaction where EmployeeID = {0} and Day = '{1}' ", employeeID, day.ToString()));


but this doesn't:
Code:
IList list = session.Find(string.Format("from ProductionLaborTransaction plt where " +
                  "plt.EmployeeID = {0} and plt.Day = '{1}' ",
                  employeeID, day.ToString()));


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