Hi Jon,
I gave this a try, initially with no luck, but if you can add a property to
your domain-model (and DB) it's not too bad (see the end for the
workaround). Here's my findings:
I was using the following domain:
public class Account : Entity
{
public virtual string Name { get; set; }
}
and the following DTO:
public class AccountSummary
{
public string Prefix;
public int PrefixCount;
}
I tried the following query (which I think should work):
AccountSummary dtoAlias = null;
var accountSummaries =
s.QueryOver<Account>()
.SelectList(l => l
.SelectGroup(a => a.Name.Substr(1, 1)).WithAlias(() =>
dtoAlias.Prefix)
.Select(Projections.RowCount()).WithAlias(() =>
dtoAlias.PrefixCount))
.TransformUsing(Transformers.AliasToBean<AccountSummary>())
.List<AccountSummary>();
This didn't work. I think it's because the .SelectGroup maps to the
ICriteria Projections.GroupProperty(), and this is (I think) restricted to
property projections, and does not handle the function projection inside it.
That's just a guess.
I tried to do this by creating the ICriteria manually:
.SelectList(l => l
.Select(Projections.Alias(Projections.GroupProperty(Projections.SqlFunction("substring",
NHibernateUtil.String, Projections.Property("Name"),
Projections.Constant(1), Projections.Constant(1))), "Prefix"))
... but I got unusual SQL that seemed to mix named and positional
parameters.
This might well be a bug in ICriteria. In addition, if ICriteria could
handle the projection of the function, then QueryOver could be updated to
handle creating the correct ICriteria. You could raise a JIRA for this.
As a workaround, I added a calculated property to the domain object:
public class Account : Entity
{
...
public virtual string Prefix { get { return Name.Substring(0, 1); } }
}
And the mapping:
<property name="Prefix" access="readonly" />
or by code:
m.Property(e => e.Prefix, pm => pm.Access(Accessor.ReadOnly))
This then simplified the query to:
var accountSummaries =
s.QueryOver<Account>()
.SelectList(l => l
.SelectGroup(a => a.Prefix).WithAlias(() => dtoAlias.Prefix)
.Select(Projections.RowCount()).WithAlias(() =>
dtoAlias.PrefixCount))
.TransformUsing(Transformers.AliasToBean<AccountSummary>())
.List<AccountSummary>();
Which worked fine. I hope that helps.
Cheers,
Richard
--
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.