2012-08-14 17 views
5

JoinAlias ​​kullanarak çeşitli varlıklar arasında bir araya gelmeye çalışıyorum ve bunlardan birden fazlasının nasıl katılacağını anlayamıyorum. Bir SQL hatası aşağıdaki kod sonuçları:NHibernate QueryOver çoklu birleştirme takma adları, yalnızca bir tane birleşim oluşturuyor

[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(); 
    } 
} 

hatası:

ERROR: 
SQLite error 
no such column: shirtstyle3_.Id 

hataya neden olan SQL:

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 */ 

Bu hata meydana neden görmek çok açıktır - Sorgu eksik, özellikle 'ShirtShirtStyle' dışındaki tüm katılımlar eksik. JoinAlias'ı anladığım kadarıyla, no'lu kodu sağladığım kodu gerekli tablolara katılmalıdır, ama nedenini anlamıyorum. Bu durumda JoinAlias ​​çalışması yapmak için daha fazla ihtiyacım var mı?

Bu sınama için oluşturduğum varlıklar ve eşlemeler, nasıl eşlendikleriyle ilgili bir şeylerin olması durumunda aşağıdadır.

Varlıkları:

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(); 
    } 
} 
+0

Tahminimce, JoinAlias ​​yerine JoinQueryOver kullanmanız gerekir. –

cevap

2

Eh bu sorgu beklendiği gibi sadece çalışması için görünür - konu Akıcı NHibernate ile gibi gözüküyor. Referanslar, Kompozit bir varlık üzerinde .KeyReference ile belirtilse bile, 'u https://stackoverflow.com/a/10995486/981205 uyarınca eşlemede yeniden belirtmeniz gerekir. Bunun bir hata veya beklenen davranış olup olmadığından emin değil.

+0

Bunun için teşekkürler, delirdiğimi düşündüm. Sinir bozucu olan şey, yalnızca sorguyu kullanıyorsanız, linq kullanıyorsanız, nhibernate birleştirme oluşturur, sorun oluşur. Sorun şu ki, dış birleştirme ihtiyacım var, dolayısıyla sorguyu kullanmak zorundayım ... – setebos

İlgili konular