2012-08-14 13 views
5

में शामिल होता है I JoinAlias ​​का उपयोग करके कई इकाइयों में शामिल होने का प्रयास कर रहा हूं, और यह पता नहीं लगा सकता कि उनमें से एक से अधिक में शामिल होने के तरीके को कैसे प्राप्त किया जाए। किसी SQL त्रुटि में निम्न कोड परिणाम:NHibernate QueryOver कई अलियास में शामिल हो जाते हैं, केवल पहला एक

[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: 
SQLite error 
no such column: shirtstyle3_.Id 

एसक्यूएल कि त्रुटि का कारण बनता है:

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

यह देखने के लिए क्यों त्रुटि होने वाली बहुत स्पष्ट है - क्वेरी में शामिल होने की गुम है, विशेष रूप से प्रत्येक 'शर्टशर्ट स्टाइल' के अलावा अन्य शामिल हों। JoinAlias ​​की मेरी समझ से, मैंने जो कोड प्रदान किया है आवश्यक तालिकाओं में शामिल होना चाहिए, लेकिन ऐसा नहीं है और मुझे समझ में नहीं आता है। क्या इस मामले में जॉइनअलीस को काम करने की ज़रूरत है?

इस परीक्षण के लिए मैंने बनाए गए संस्थाओं और मैपिंग्स नीचे दिए गए हैं, अगर उनके पास मैप किए जाने के तरीके के साथ कुछ करना है।

संस्थाओं:

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; } 
    } 

मानचित्र:

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

मेरा अनुमान है कि आपको JoinAlias ​​के बजाय JoinQueryOver का उपयोग करना चाहिए। –

उत्तर

2

खैर इस क्वेरी सिर्फ कार्य करने के लिए प्रकट होता है के रूप में उम्मीद - मुद्दा धाराप्रवाह NHibernate के साथ लग रहा है। भले ही संदर्भ एक समग्र इकाई पर .KeyReference के माध्यम से निर्दिष्ट किए गए हों, आपको स्पष्ट रूप से मैपिंग में प्रति https://stackoverflow.com/a/10995486/981205 में दोबारा निर्दिष्ट करने की आवश्यकता है। सुनिश्चित नहीं है कि यह एक बग या अपेक्षित व्यवहार है।

+0

इसके लिए धन्यवाद, मैंने सोचा कि मैं पागल हो रहा था। कष्टप्रद बात यह है कि समस्या तब होती है जब आप क्वेरी का उपयोग कर रहे हैं, यदि आप linq का उपयोग करते हैं, तो nhibernate जुड़ता है। समस्या यह है कि मुझे बाहरी जोड़ों की आवश्यकता है, इसलिए मुझे क्वेरी का उपयोग करना होगा ... – setebos

संबंधित मुद्दे