Are you looking for something like this ?
public static void AddOrderBy(DetachedCriteria criteria, string orderByPath)
{
string alias = GetAlias(criteria, orderByPath);
criteria.AddOrder(Order.Asc(alias));
}
public static string GetAlias(DetachedCriteria criteria, string path)
{
ISessionFactoryImplementor sessionFactoryImpl =
(ISessionFactoryImplementor)YOUR_SESSION_FACTORY;
IPropertyMapping parentMapping =
(IPropertyMapping)sessionFactoryImpl.GetEntityPersister(criteria.EntityOrClassName);
string propertyFullName = "";
string fullQualifier = "";
string fullQualifierAlias = "";
foreach (string propertyName in path.Split('.'))
{
propertyFullName += propertyName;
IType propertyType = parentMapping.ToType(propertyFullName);
if (propertyType.IsAssociationType)
{
// the property is an association
IAssociationType associationType =
(IAssociationType)propertyType;
string associatedEntityName =
associationType.GetAssociatedEntityName(sessionFactoryImpl);
parentMapping =
(IPropertyMapping)sessionFactoryImpl.GetEntityPersister(associatedEntityName);
fullQualifier = StringHelper.Qualify(fullQualifier,
propertyFullName);
// ensure there is an alias to this association
DetachedCriteria child =
criteria.GetCriteriaByPath(fullQualifier);
if (child != null)
{
// there is already a criteria to the path
// -> use the criteria alias
fullQualifierAlias = child.Alias;
}
else
{
// there is no criteria to this path
// -> create a new one
fullQualifierAlias = fullQualifier.Replace('.', '_');
criteria.CreateAlias(fullQualifier, fullQualifierAlias);
}
propertyFullName = "";
}
else if (propertyType.IsComponentType)
{
// the property is a component
propertyFullName += ".";
}
else
{
// the property is a "basic" property
return StringHelper.Qualify(fullQualifierAlias,
propertyFullName);
}
}
throw new ArgumentException();
}
I did not tested it but you get the idea.
On Tue, Mar 9, 2010 at 4:25 PM, F.B. ten Kate <[email protected]> wrote:
> Anyone? Atm i've patched it with a quick and dirty find and replace
> but that's far from ideal.
>
> On 9 mrt, 12:44, "F.B. ten Kate" <[email protected]> wrote:
> > I'm running into problems with this relatively easy task.
> >
> > We are setting up webservices that can get called with RequestOptions
> > which allow for dynamic and generic setting of the DetachedCriteria
> > properties (Adding criteria and Orderby's max results etc).
> >
> > So far this has been working great but atm i'm running into a problem
> > with a Object that has nested alliases.
> >
> > We create base DetachedCriteria where the user's options are added to,
> > meaning i have to setup aliases where needed.
> >
> > //Set the debtor criteria
> > DetachedCriteria criteria =
> > DetachedCriteria.For(typeof(Invoice))
> > .CreateAlias("Debtor", "Debtor")
> > .CreateAlias("Reservation.Reason",
> > "Reservation.Reason", JoinType.LeftOuterJoin)
> > .Add(Expression.Eq("Debtor.Account.ID", accountID));
> >
> > The problem is the webservice users add orderby's based on the XML i
> > give them, i reflect the actual propertynames back from the xml (for
> > example: reservation.reservationreason.text would be reflected to
> > Reservation.Reason.Text since those are my property names).
> >
> > Here in lies the problem, The orderby would be added with on the
> > property: Reservation.Reason.Text which NHibernate cannot resolve....
> >
> > If i change the Alias
> > into: .CreateAlias("Reservation.Reason",
> > "ReservationReason", JoinType.LeftOuterJoin) and manually changing the
> > orderby into ReservationReason.Text the code executes.
> >
> > I want my code to be as reusable as possible so i don't want to write
> > exceptions in my code where i overwrite the reflection part and use
> > static strings like "ReservationReason".
> >
> > Since it seems NHibernate does not like "dots" in the alias name the
> > only other option in my eyes would be to determine if the class is a
> > association (join, many-to-one etc) or just a component/property. If
> > it's the First it should remove the "dots" from the reflected property
> > name (this can be made reusable and automated) but if it's a component
> > it should stay as it is since then NHibernate does understand the dots
> > etc.
> >
> > Does anyone have any bright ideas or pushes in the right direction?
> >
> > (I didn't add my mappings since i dont think they are needed for this
> > particular problem, if they are i will post them when requested.)
>
> --
> 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]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
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.