Nhibernate – c.Orders.elements is not mapped

Continuing to Work through Steve Bohlen’s Summer of Nhibernate series and encountered a problem with session 6’s HQL example of obtaining a collection of child objects from the database.

The HQL shown in the screen cast was :

"select distinct c from Customer c , c.Orders.elements o where o.OrderDate > :date"

Using this HQL gave me the following error :

c.Orders.elements is not mapped

The syntax for obtaining the elements in the child collection has changed, and the correct syntax to be used is :

elements(c.Orders)

This didn’t work for me either though (NHibernate 2.1.2.4000), I was getting the following error instead :

Exception of type 'Antlr.Runtime.MissingTokenException'

I wasn’t getting very far with this problem, I couldn’t find a good example of HQL doing the same thing and I’m a bit anal in that I didn’t want to proceed with the rest of the screen cast while I had this up in the air.  In reality I doubt I’d ever use HQL but I still wanted to sort the problem.    In the end I’ve used the following HQL with an explicit inner join and fetch for the orders collection :

"Select distinct c from Customer c inner join fetch c.Orders o
      where o.OrderDate > :date"

This works great but I’m wondering if a shorter HQL statement will do the same thing as Steve demo’ed in his screen cast so I’m going to post a query on the Nhibernate group.   I’ll follow this post up once I’ve got a better solution.

** Update – 14th December 2009 **

I posted the query in the NHibernate user group and have received a response.   The problem was I wasn’t using the keyword “as” when giving the order collection an alias.   The hql to use is below :

"select distinct c from Customer c , elements(c.Orders) as o where o.OrderDate > :date"
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s