Skip to content

Component's Parent property is not accessible in queries #1583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SlyNet opened this issue Feb 23, 2018 · 3 comments · Fixed by #3024
Closed

Component's Parent property is not accessible in queries #1583

SlyNet opened this issue Feb 23, 2018 · 3 comments · Fixed by #3024

Comments

@SlyNet
Copy link
Contributor

SlyNet commented Feb 23, 2018

Having mapped list collection with composite-element its not possible to use parent property in query.
Domain:

	public class Parent
	{
		public virtual int ParentId { get; set; }
		public virtual IList<Child> Children { get; set; }
	}

	public class Child
	{
		public virtual Parent ParentLink { get; set; }
	}

Mapping:

mapper.Class<Parent>(rc => {
	rc.Id(x => x.ParentId, m => m.Generator(Generators.HighLow));
	rc.List(
		x => x.Children,
		listMap =>
		{
			listMap.Table("Children");
			listMap.Index(index => index.Column("Position"));

			listMap.Key(keyMap => { keyMap.Column(
				clm =>
				{
					clm.Name("ParentId");
				});
			});
			listMap.Lazy(CollectionLazy.Lazy);
			listMap.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.All);
			listMap.Inverse(true);
		},
		rel => { rel.Component(cmp => { cmp.Parent(x => x.ParentLink); }); }
	);
});

query:

var result = (from p in session.Query<Parent>().SelectMany(x => x.Children)
	select p.ParentLink.ParentId).ToList();

Expected result: query returns list of ParentIds that have any Children
Actual result: query fails with exception

Hibernate.QueryException : could not resolve property: ParentLink of: component[] [.Select[NHibernate.Test.NHSpecificTest.GH1583.Child, NHibernate.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null,System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089](.SelectMany[NHibernate.Test.NHSpecificTest.GH1583.Parent, NHibernate.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null,NHibernate.Test.NHSpecificTest.GH1583.Child, NHibernate.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null](NHibernate.Linq.NhQueryable`1[NHibernate.Test.NHSpecificTest.GH1583.Parent], Quote((x, ) => (x.Children)), ), Quote((p, ) => (p.ParentLink.ParentId)), )]
   at NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName)
   at NHibernate.Hql.Ast.ANTLR.Tree.FromElementType.GetPropertyType(String propertyName, String propertyPath)
   at NHibernate.Hql.Ast.ANTLR.Tree.DotNode.GetDataType()
   at NHibernate.Hql.Ast.ANTLR.Tree.DotNode.Resolve(Boolean generateJoin, Boolean implicitJoin, String classAlias, IASTNode parent)
   at NHibernate.Hql.Ast.ANTLR.Tree.DotNode.ResolveFirstChild()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.propertyRefPath()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.propertyRef()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectExpr()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectExprList()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectClause()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.unionedQuery()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.query()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectStatement()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.statement()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(String collectionRole)
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
   at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query)
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
...
@fredericDelaporte
Copy link
Member

I do not understand this mapping. Are you sure it is valid? I do not see any component in your classes.

@fredericDelaporte
Copy link
Member

Ok, so you map Child as a component and Parent.Children as a list of component, but want it bi-directional, and want to query the component parent.

Quite a convoluted case, and I do not see any reason for supporting this: what is the point of "round-tripping" "parent=>children=>parent" in queries? Since Child is mapped as a component, any query is forced to query from Parent for accessing Child.

@hazzik
Copy link
Member

hazzik commented Jun 20, 2018

It is a new feature. Querying parent of the component is not supported. Even in Hibernate.

@hazzik hazzik changed the title Parent property is not accessible in queries Component's Parent property is not accessible in queries Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants