NHibernate.QueryException : duplicate association path

985 views
Skip to first unread message

Neil McLaughlin

unread,
Nov 2, 2011, 10:33:43 AM11/2/11
to nhu...@googlegroups.com
The code fragment below describes what I want to do using criteria but it blows up with the above error.

There is a 1 to many relationship between Buildings and AttributeValues and my aim is to find all Buildings which have an AttributeValue of "Large" and an AttributeValue of "Blue".

            var attributeValueAlias1 = new AttributeValue();
            var attributeValueAlias2 = new AttributeValue();

            var result = repository
                .CreateCriteriaFor<Buildings>()
                .Join(o=>o.AttributeValues, ()=> attributeValueAlias1)
                .Join(o=>o.AttributeValues, ()=> attributeValueAlias2)
                .Add(() => attributeValueAlias1.Value == "Large")
                .Add(() => attributeValueAlias2.Value == "Blue")
                .List();

There are few posts on this forum and on other sites but none with a solution to the problem which is described here and here

I can make it so a test is added to see if the alias exists using _criteria.GetCriteriaByAlias(alias) before adding it so the Duplicate alias error does not occur but then there is only a single join and it results in a SQL Where clause which will never be true

    SELECT *
    FROM Buildings  this_
        inner join AttributeValues attributev1_
        on this_.Id = attributev1_.BuildingId
    WHERE 
        attributev1_.Value = 'Large'
        and attributev1_.Value = 'Blue'

It is not a complicated or unusual query so I'd be surprised if there isn't a work around.

We are using NHibernate 2.1 

Thanks in advance

Neil

Note: this question has also been posted to StackOverflow here 

John Davidson

unread,
Nov 2, 2011, 1:23:13 PM11/2/11
to nhu...@googlegroups.com
What is the sql when you try

 var result = repository
                .CreateCriteriaFor<Buildings>()
                .Join(o1=>o1.AttributeValues, ()=> attributeValueAlias1)
                .Join(o2=>o2.AttributeValues, ()=> attributeValueAlias2)
                .Add(() => attributeValueAlias1.Value == "Large")
                .Add(() => attributeValueAlias2.Value == "Blue")
                .List();

John Davidson


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/a2psEeLMbHYJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Neil McLaughlin

unread,
Nov 3, 2011, 5:18:56 AM11/3/11
to nhu...@googlegroups.com
Good suggestion. Even with the change it continues to generate the exception.

Just for interest I have created an HQL query which achieves the desired results but want to avoid have to rewrite our dynamic querying module to change from criteria to HQL if possible.

Cheers

Neil

Neil McLaughlin

unread,
Nov 2, 2011, 7:26:55 PM11/2/11
to nhusers
Thanks for the reply.

Good suggestion but even after making the suggested change to the
joins the query still generates the "NHibernate.QueryException :
duplicate association path: AttributeValues" exception.

Just for info I have managed to create an equivalent working query
using HQL but would prefer not to have to move our dynamic criteria
based querying over to HQL if possible.

Cheers

Neil

On Nov 2, 5:23 pm, John Davidson <jwdavid...@gmail.com> wrote:
> What is the sql when you try
>
>  var result = repository
>                 .CreateCriteriaFor<Buildings>()
>                 .Join(o1=>o1.AttributeValues, ()=> attributeValueAlias1)
>                 .Join(o2=>o2.AttributeValues, ()=> attributeValueAlias2)
>                 .Add(() => attributeValueAlias1.Value == "Large")
>                 .Add(() => attributeValueAlias2.Value == "Blue")
>                 .List();
>
> John Davidson
>
> On Wed, Nov 2, 2011 at 10:33 AM, Neil McLaughlin <
>
>
>
>
>
>
>
> n...@echelon-solutions.co.uk> wrote:
> > The code fragment below describes what I want to do using criteria but it
> > blows up with the above error.
>
> > There is a 1 to many relationship between Buildings and AttributeValues
> > and my aim is to find all Buildings which have an AttributeValue of "Large"
> > *and* an AttributeValue of "Blue".
>
> >             var attributeValueAlias1 = new AttributeValue();
> >             var attributeValueAlias2 = new AttributeValue();
>
> >             var result = repository
> >                 .CreateCriteriaFor<Buildings>()
> >                 .Join(o=>o.AttributeValues, ()=> attributeValueAlias1)
> >                 .Join(o=>o.AttributeValues, ()=> attributeValueAlias2)
> >                 .Add(() => attributeValueAlias1.Value == "Large")
> >                 .Add(() => attributeValueAlias2.Value == "Blue")
> >                 .List();
>
> > There are few posts on this forum and on other sites but none with a
> > solution to the problem which is described here<http://stackoverflow.com/questions/5940922/duplicate-association-path...> and
> > here<http://blog.dezfowler.com/2008/06/duplicate-association-path-bug-in.html>
>
> > I can make it so a test is added to see if the alias exists using
> > _criteria.GetCriteriaByAlias(alias) before adding it so the Duplicate alias
> > error does not occur but then there is only a single join and it results in
> > a SQL Where clause which will never be true
>
> >     SELECT *
> >     FROM Buildings  this_
> >         inner join AttributeValues attributev1_
> >         on this_.Id = attributev1_.BuildingId
> >     WHERE
> >         attributev1_.Value = 'Large'
> >         and attributev1_.Value = 'Blue'
>
> > It is not a complicated or unusual query so I'd be surprised if there
> > isn't a work around.
>
> > We are using NHibernate 2.1
>
> > Thanks in advance
>
> > Neil
>
> > Note: this question has also been posted to StackOverflow here<http://stackoverflow.com/questions/7981027/nhibernate-queryexception-...>
Reply all
Reply to author
Forward
0 new messages