tags:

views:

1405

answers:

1

Hi

I am using NHibernate version 2.0.0.4000.

In one of my queries I wanted to make use of the sql function dateadd to add a number of days. This wasn't registered so I created my own dialect and registered the function as follows:

RegisterFunction("adddays", 
    new SQLFunctionTemplate(NHibernateUtil.DateTime, 
    "dateadd(dd, ?1, ?2)"));

The registration gets hit and seems to work fine. I use the function in a DetachedCriteria query as follows:

...
Restrictions.LtProperty("DateColumn1"
    Projections.SqlFunction("adddays", NHibernateUtil.DateTime,
        Projections.Constant(days),
        Projections.Property("DateColumn2"))
...

The criteria is returned from a method and passed of to another query. Upon the execution of the final query I get the following exception:

NHibernate.MappingException was caught
  Message="No persister for: NHibernate.Criterion.SqlFunctionProjection"
  Source="NHibernate"
  StackTrace:
       at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName,
            Boolean throwIfNotFound)
       at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
       ...

None of the blog posts that I have seen mention this problem. Can anybody help?

Cheers in advance. Nige.

+4  A: 

Solved it.

The problem was caused by my usage elsewhere of Restrictions.Eq rather than Restrictions.EqProperty. The former does not have an overload for (IProjection,IProjection) and so was treating the second projection as an object and passing it to a persister.

Thanks to anyone who investigated this. Nigel.

Nigel