Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate QueryOver multiple join aliases, only first one generates a join

Tags:

c#

nhibernate

I am attempting to join across several entities using JoinAlias, and can't figure out how to get more than one of them to join. The following code results in a SQL error:

[TestMethod]
public void TestAliases()
{
    App_Start.NHibernateProfilerBootstrapper.PreStart();

    var type = new ShirtStyleType {Id = 1};
    var style = new ShirtStyle {Id = 1, ShirtStyleType = type};
    var shirt = new Shirt {Id = 1};
    var shirtStyle = new ShirtShirtStyle {Shirt = shirt, ShirtStyle = style};
    shirt.ShirtStyles = new[] {shirtStyle};

    using (Session.BeginTransaction())
        Session.Save(shirt);

    Session.Clear();

    using (Session.BeginTransaction())
    {
        Shirt shirtAlias = null;
        ShirtShirtStyle shirtStylesAlias = null;
        ShirtStyle shirtStyleAlias = null;
        ShirtStyleType shirtStyleTypeAlias = null;

        var query = Session.QueryOver<Shirt>(() => shirtAlias)
            .JoinAlias(() => shirtAlias.ShirtStyles, () => shirtStylesAlias)
            .JoinAlias(() => shirtStylesAlias.ShirtStyle, () => shirtStyleAlias)
            .JoinAlias(() => shirtStyleAlias.ShirtStyleType, () => shirtStyleTypeAlias)
            .Where(() => shirtStyleTypeAlias.Id == 1)
            .List();
    }
}

The error:

ERROR: 
SQLite error
no such column: shirtstyle3_.Id

The SQL that causes the error:

SELECT this_.Id                as Id0_1_,
       shirtstyle1_.Shirt      as Shirt1_0_,
       shirtstyle1_.ShirtStyle as ShirtStyle1_0_
FROM   "Shirt" this_
       inner join "ShirtShirtStyle" shirtstyle1_
         on this_.Id = shirtstyle1_.Shirt_id
WHERE  shirtstyle3_.Id = 1 /* @p0 */

It's very obvious to see why the error is occurring - the query is missing joins, specifically every join other than 'ShirtShirtStyle'. From my understanding of JoinAlias, the code I provided should be joining the necessary tables, but it isn't and I don't understand why. Is there something more I need to make JoinAlias work in this case?

The entities and mappings I created for this test are below, in case it has something to do with how they are mapped.

Entities:

public class Shirt
{
    public virtual int Id { get; set; }
    public virtual IList<ShirtShirtStyle> ShirtStyles { get; set; } 
}

public class ShirtShirtStyle
{
    public virtual Shirt Shirt { get; set; }
    public virtual ShirtStyle ShirtStyle { get; set; }

    protected bool Equals(ShirtShirtStyle other)
    {
        return Equals(Shirt, other.Shirt) && Equals(ShirtStyle, other.ShirtStyle);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((ShirtShirtStyle) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((Shirt != null ? Shirt.GetHashCode() : 0)*397) ^ (ShirtStyle != null ? ShirtStyle.GetHashCode() : 0);
        }
    }
    }

    public class ShirtStyle
    {
        public virtual int Id { get; set; }
        public virtual ShirtStyleType ShirtStyleType { get; set; }
    }

    public class ShirtStyleType
    {
        public virtual int Id { get; set; }
    }

Maps:

public class ShirtMap : ClassMap<Shirt>
{
    public ShirtMap()
    {
        Id(x => x.Id).GeneratedBy.Assigned();
        HasMany(x => x.ShirtStyles);
    }
}

public sealed class ShirtShirtStyleMap : ClassMap<ShirtShirtStyle>
{
    public ShirtShirtStyleMap()
    {
        CompositeId()
            .KeyReference(x => x.Shirt)
            .KeyReference(x => x.ShirtStyle);
    }
}

public sealed class ShirtStyleMap : ClassMap<ShirtStyle>
{
    public ShirtStyleMap()
    {
        Id(x => x.Id).GeneratedBy.Assigned();
        References(x => x.ShirtStyleType);
    }
}

public sealed class ShirtStyleTypeMap : ClassMap<ShirtStyleType>
{
    public ShirtStyleTypeMap()
    {
        Id(x => x.Id).GeneratedBy.Assigned();
    }
}
like image 630
heyseuss Avatar asked Nov 04 '22 18:11

heyseuss


1 Answers

Well this query appears to function just as expected - the issue looks to be with Fluent NHibernate. Even though the references are specified on a composite entity via .KeyReference, you need to explicitly re-specify them in the mapping, per https://stackoverflow.com/a/10995486/981205. Not sure if this is a bug or expected behavior.

like image 118
heyseuss Avatar answered Nov 10 '22 01:11

heyseuss