MisterTom,

Thank you so much!!! The ValueExpression does exactly what I need to
do.

Hardcoding aliases was not an option because if there are other
dynamically generated restrictions that caused the alias to change.

I will try to reproduce the alias bug in a small test solution and
will submit it to Nhibernate Jira.

Thanks again,
Dmitry

On Feb 28, 12:34 pm, MisterTom <[email protected]> wrote:
> > Does it sound like an NHibernate bug?
>
> Yes, the SQL alias sounds like a bug. You will have to find a
> workaround :
>
> - You can hard-code the alias in the SQL criterion
> var criteria = session.CreateCriteria<Company>()
>         .CreateAlias("Notes", "Notes")
>         .Add(Expression.Sql("notes3_.note = ?", noteToFind,
> NHibernateUtil.String));
>
> - You can remove the alias (it works fine if you do not have column
> name conflicts)
> var criteria = session.CreateCriteria<Company>()
>         .CreateAlias("Notes", "Notes")
>         .Add(Expression.Sql("note = ?", noteToFind, NHibernateUtil.String));
>
> - You can use an "exists" statement :
> var criteria = session.CreateCriteria<Company>()
>     .Add(new ValueExpression("Notes", noteToFind, "="));
>
> [Serializable]
> public class ValueExpression : AbstractCriterion
> {
>         private readonly string collectionName;
>         private readonly string op;
>         private readonly object value;
>
>         public ValueExpression(string collectionName, object value, string
> op)
>         {
>                 this.collectionName = collectionName;
>                 this.value = value;
>                 this.op = op;
>         }
>
>         public override IProjection[] GetProjections()
>         {
>                 return null;
>         }
>
>         public override TypedValue[] GetTypedValues(ICriteria criteria,
> ICriteriaQuery criteriaQuery)
>         {
>                 ICollectionPersister persister =
> (IQueryableCollection)criteriaQuery.Factory.GetCollectionPersister(criteriaQuery.GetEntityName(criteria)
> + "." + this.collectionName);
>                 return new TypedValue[] { new 
> TypedValue(persister.ElementType,
> this.value, EntityMode.Poco) };
>         }
>
>         public override SqlString ToSqlString(ICriteria criteria,
> ICriteriaQuery criteriaQuery, IDictionary<string, IFilter>
> enabledFilters)
>         {
>                 IQueryableCollection persister =
> (IQueryableCollection)criteriaQuery.Factory.GetCollectionPersister(criteriaQuery.GetEntityName(criteria)
> + "." + this.collectionName);
>
>                 criteriaQuery.AddUsedTypedValues(this.GetTypedValues(criteria,
> criteriaQuery));
>
>                 string tableName = ((IJoinable)persister).TableName;
>
>                 SqlStringBuilder builder = new SqlStringBuilder(20);
>                 builder.Add("exists ( select 1 from ").Add(tableName);
>                 builder.Add(" where ").Add(persister.KeyColumnNames[0]).Add(" 
> =
> ").Add(criteriaQuery.GetIdentifierColumns(criteria)[0]);
>                 builder.Add(" and
> ").Add(persister.ElementColumnNames[0]).Add(this.op).AddParameter().Add(" )");
>
>                 return builder.ToSqlString();
>         }
>
>         public override string ToString()
>         {
>                 return (this.collectionName + this.op + this.value);
>         }
>
> }

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to