Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate using wrong table alias

I am trying to filter a collection based on a foreign key. I have two classes which are mapped with

public class GroupPriceOverrideMap:ClassMap<GroupPriceOverride>
    {
        public GroupPriceOverrideMap()
        {
            CompositeId()
                .KeyReference(x => x.Service,"ServiceCode")
                .KeyReference(x => x.CustomerAssetGroup, "GroupID");

            Map(x => x.Price);

            Table("accGroupPriceOverride");
        }
    }

public class CustomerAssetGroupMap:ClassMap<CustomerAssetGroup>
    {
        public CustomerAssetGroupMap()
        {
            Id(x => x.GroupID).Unique();

            Map(x => x.Description);

            References(x => x.Customer).Column("CustomerID");

            HasMany<GroupPriceOverride>(x => x.PriceOverrides).KeyColumn("GroupID");

            Table("accCustAssetGroup");
        }
    }

I query it using

_session.Linq<GroupPriceOverride>.Where(x => x.CustomerAssetGroup.GroupID == groupID)

However this is generating

SELECT this_.ServiceCode as ServiceC1_9_0_, this_.GroupID as GroupID9_0_, this_.Price as Price9_0_ FROM accGroupPriceOverride this_ WHERE customeras1_.GroupID = @p0

there where clause is referencing a table alias which doesn't exist(customeras1). This is probably an alias for crossing with customerassetgroup but there is no need to perform that cross. I'm sure that it is just something in my mapping with is wrong but I can't find it. I've tried various column renaming in case the presence of GroupID in both tables was causing problems but that didn't fix it. Any ideas?

Edit I found that if I queried doing

_session.Linq<CustomerAssetGroup>().Where(x => x.GroupID == groupID).FirstOrDefault().PriceOverrides;

then I got the correct result. I also found that if I saved a GroupPriceOverride and then queried for it using HQL then it wouldn't be found but I could still find the entity by loading the parent and looking at its collection of overrides.

_session.CreateQuery("FROM GroupPriceOverride i").List().Count;//returns 0
_session.CreateQuery("FROM CustomerAssetGroupi").List().FirstOrDefault().PriceOverrides.Count;//returns 1
like image 323
stimms Avatar asked Nov 14 '22 05:11

stimms


1 Answers

Looks like a bug in the old LINQ provider. Could you file a bug here:

https://nhibernate.jira.com/secure/Dashboard.jspa

You might be able to get around it via:

_session.Linq<GroupPriceOverride>.Where(x => x.CustomerAssetGroup == group)

and let NHibernate figure out the ID. If you don't have the group already, you could do this:

var group = _session.Load<CustomerAssetGroup>(groupID);
_session.Linq<GroupPriceOverride>.Where(x => x.CustomerAssetGroup == group)

The ISession.Load(id) will only generate a proxy, but won't actually hit the database until you access a property (which you wouldn't be since you're just using it to specify the ID).

like image 56
James Kovacs Avatar answered Dec 26 '22 09:12

James Kovacs