English 中文(简体)
NHibernate Criteria using Projections for Substring with in clause
原标题:

I had a scenario in Oracle where i need to match a substring part of column with a list of values. i was using sqlfunction projection for applying the substring on the required column, and added that projection as part of an In Clause Restriction. Below is the simplified criteria i wrote for that.

ICriteria criteriaQuery = session.CreateCriteria<Meeting>()
    .Add(Restrictions.In(
        Projections.SqlFunction(
            "substring",
            NHibernateUtil.String,
            Projections.Property("Code"),
            Projections.Constant(1),
            Projections.Constant(3)),
        new string[] { "D01", "D02" }))
        .Add(Restrictions.In("TypeId", meetingTypes));

The problem that i had with this was that the generated SQL was wrong, where the number of parameters registered for the statement are more than what the statement actually uses and some parameters are repeated even though they are not used. This causes the statement to fail with the message - ORA-01036: illegal variable name/number. Generated Query

SELECT this_.Meeting_id as Meeting1_0_2_, .....  
WHERE substr(this_.Mcs_Main, :p0, :p1) in (:p2, :p3) 
and this_.Meeting_Type_Id in (:p4, :p5);
:p0 = 1, :p1 = 3, :p2 = 1, :p3 = 3, :p4 =  D02 , :p5 =  D03 , :p6 = 101, :p7 = 102

p2 and p3 are generated again and are duplicates of p0, p1 because of which the entire query is failing.

I was able to temporarily resolve this by mapping a a new property with a formula, but i don t think that is the right approach since the formula will be executed always even when i don t need the substring to be evaluated.

Any suggestions on whether projections work fine when used with the combination of In clause, the same projection works fine when i use Equal Restriction and not In.

问题回答

This bug is fixed in 3.0.0.GA version.





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签