From: Andrei A. (JIRA) <nh...@gm...> - 2011-05-24 11:01:44
|
Allow query on .id special accessor on <any> mappings via Linq -------------------------------------------------------------- Key: NH-2735 URL: http://216.121.112.228/browse/NH-2735 Project: NHibernate Issue Type: Improvement Components: Core Affects Versions: 3.2.0Beta1 Reporter: Andrei Alecu Querying a property defined with an <any> mapping by its id needs to use the special .class and .id properties defined by HQL. Not having a way to access .id makes it impossible to translate queries such as the following to Linq: from ToyBox t where t.Shape.class = Square and t.Shape.id = 1 (note that there's currently a bug with .class at NH-2734) Note that accessing the identifier property mapped as .Id (non lowercase) does not work, the HQL query parser complains that there's an access to an unmapped property .Id. My current workaround is to define this extension method as a custom linq generator: public class ImplicitIdGenerator : BaseHqlGeneratorForMethod { public ImplicitIdGenerator() { this.SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<object>(x => x.ImplicitId()) }; } public override HqlTreeNode BuildHql( MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) { return treeBuilder.Dot(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Ident("id")); } } public static class NhibernateLinqExtensions { public static object ImplicitId(this object what) { throw new NotImplementedException("Used by NHibernate linq with <any> mappings."); } } I believe the special .id property may also be used in other circumstances when the name of the property used for the identifier is not known. I'm not sure if this is the most elegant implementation, but this code lets you do this: var results = session.Query<ToyBox>().Where(x=> /* class constraint, see NH-2328 */ && x.Shape.ImplicitId() == 1); -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://216.121.112.228/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira |
From: Fabio M. (JIRA) <nh...@gm...> - 2011-05-24 12:28:44
|
[ http://216.121.112.228/browse/NH-2735?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Fabio Maulo updated NH-2735: ---------------------------- Component/s: (was: Core) Linq Provider > Allow query on .id special accessor on <any> mappings via Linq > -------------------------------------------------------------- > > Key: NH-2735 > URL: http://216.121.112.228/browse/NH-2735 > Project: NHibernate > Issue Type: Improvement > Components: Linq Provider > Affects Versions: 3.2.0Beta1 > Reporter: Andrei Alecu > > Querying a property defined with an <any> mapping by its id needs to use the special .class and .id properties defined by HQL. > Not having a way to access .id makes it impossible to translate queries such as the following to Linq: > from ToyBox t where t.Shape.class = Square and t.Shape.id = 1 (note that there's currently a bug with .class at NH-2734) > Note that accessing the identifier property mapped as .Id (non lowercase) does not work, the HQL query parser complains that there's an access to an unmapped property .Id. > My current workaround is to define this extension method as a custom linq generator: > public class ImplicitIdGenerator : BaseHqlGeneratorForMethod > { > public ImplicitIdGenerator() > { > this.SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<object>(x => x.ImplicitId()) }; > } > public override HqlTreeNode BuildHql( > MethodInfo method, > Expression targetObject, > ReadOnlyCollection<Expression> arguments, > HqlTreeBuilder treeBuilder, > IHqlExpressionVisitor visitor) > { > return treeBuilder.Dot(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Ident("id")); > } > } > public static class NhibernateLinqExtensions > { > public static object ImplicitId(this object what) > { > throw new NotImplementedException("Used by NHibernate linq with <any> mappings."); > } > } > I believe the special .id property may also be used in other circumstances when the name of the property used for the identifier is not known. > I'm not sure if this is the most elegant implementation, but this code lets you do this: > var results = session.Query<ToyBox>().Where(x=> /* class constraint, see NH-2328 */ && x.Shape.ImplicitId() == 1); -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://216.121.112.228/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira |