tags:

views:

27

answers:

3

Before talking about the problem here's some background:

  1. There are 3 assemblies.
    1. One containing an Entity class and the problem HQL.
    2. Tests.
    3. Website.
  2. All the tests in the Tests assembly for are passing for the HQL.
  3. The exception is thrown in the website.
  4. If I use the equivalent Criteria code it works fine in all assemblies calling it.

Onto the problem. When calling the following HQL

var commentCount = 
    session.CreateQuery("select e.CommentCount from Entity e where e.Id = :entityId")
    .SetParameter("entityId", string.Format("{0}:{1}", entityType, entityId))
    .UniqueResult<int>();

this exception is thrown:

Entity is not mapped [select e.CommentCount from Entity e where e.Id = :entityId]

As mentioned I can just change to using Criteria, but I'm worried there might be an underlying problem that the use of Criteria is covering up.

Any ideas? Thanks.

A: 

Use C#'s own as string :

var commentCount =  session.CreateQuery("select e.CommentCount from Entity e where e.Id = :entityId and rownum <= 1")
                           .SetParameter("entityId", string.Format("{0}:{1}", entityType, entityId))
                           .UniqueResult() as string;

Do note that you might have to add the and rownum <= 1 clause at the end.

Rafael Belliard
I'm unsure where you mean I should put "as string"? "where e.Id = :entityId as string" and rownum <= 1? Thanks
Castrohenge
@Kieranmaine in my modification, notice UniqueResult<int>() changes to UniqueResult() as string.
Rafael Belliard
@Rafael - Got things working without your change. I think not adding the assembly was the bigger issue. Thanks for the help.
Castrohenge
A: 

I forgot to add the assembly to my hibernate-configuration!

Here's the code:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="demo">
        <mapping assembly="Users" />
    </session-factory>
</hibernate-configuration>

I'm not sure why criteria worked. I'm guessing it has something to do with the type being explicitly defined.

Castrohenge
Did you try my recommendation? That always works for me and I got it from my current production code.
Rafael Belliard
Criteria returns empty results for unmapped entities. Which means it WASN'T working, it just wasn't throwing.
Diego Mijelshon
Good to know. I'll look for this in the future.
Castrohenge
A: 

Add auto-import="true" to your hibernate-mapping elements. That should take care of it.

Diego Mijelshon
Thanks. I've give that try tomorrow at work.
Castrohenge
This didn't seem to work for me.
Castrohenge