I am trying to join several entities using JoinAlias and cannot figure out how to attract more than one of them. The following code results in an 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();
}
}
Error:
ERROR:
SQLite error
no such column: shirtstyle3_.Id
SQL causing 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
It is very clear why the error occurs - there are no connections in the request, in particular, every connection other than "ShirtShirtStyle". From my understanding of JoinAlias, the code I provided should connect to the required tables, but this is not the case, and I do not understand why. Is there anything else I need to do JoinAlias in this case?
The entities and comparisons that I created for this test are given below, in case this has anything to do with how they are displayed.
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; }
}
Cards:
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();
}
}