2011-09-16 40 views
1

我必須映射類:的nHibernate組僅給出一個結果

<class name="Business.DomainObjects.Institutions.Institutions, Business.DomainObjects" 
    table="Institutions" lazy="false" mutable="false" > 
<id name="Id" access="field.camelcase-underscore" column="ID"> 
    <generator class="identity" /> 
</id> 

<property name="Caption" type="String" access="field.camelcase-underscore" column="Name" not-null="true"/> 
<property name="AddressLine" type="String" access="field.camelcase-underscore" column="AddressLine" not-null="true" /> 
<property name="Phone" type="String" access="field.camelcase-underscore" column="Phone" not-null="false" /> 
<set name="WorkTime" cascade="none" fetch="join"> 
    <key column="ID" not-null="true"/> 
    <one-to-many class="Business.DomainObjects.Institutions.WorkHours, Business.DomainObjects"/> 
</set> 

<class name="Business.DomainObjects.Institutions.WorkHours, Business.DomainObjects" 
table="WorkHours" lazy="false" mutable="false" > 
<id name="Id" access="field.camelcase-underscore" column="ID"> 
    <generator class="identity" /> 
</id> 
<property name="Weekday" type="int" access="field.camelcase-underscore" column="Weekday" not-null="true" /> 
<property name="OpenFrom" type="DateTime" access="field.camelcase-underscore" column="OpenFrom" not-null="true" /> 
<property name="OpenTill" type="DateTime" access="field.camelcase-underscore" column="OpenTill" not-null="true" /> 
<property name="OpenTime" type="String" access="field.camelcase-underscore" column="OpenTime" not-null="true" /> 
<many-to-one name="Institution" class="Business.DomainObjects.Institutions.Institution, Business.DomainObjects" column="ID" /> 
</class> 

映射類代碼:

public class WorkHours 
{ 
    private int _id = 0; 
    private int _weekday = 0; 
    private DateTime _openFrom = DateTime.MinValue; 
    private DateTime _openTill = DateTime.MinValue; 
    private string _openTime = null; 
    private Institution _institution = null; 

    public WorkHours(){} 

    public int Id { get { return _id; } internal set { _id = value; } } 
    public string OpenTime { get { return _openTime; } set { _openTime = value; } } 
    public DateTime OpenTill { get { return _openTill; } set { _openTill = value; } } 
    public DateTime OpenFrom { get { return _openFrom; } set { _openFrom = value; } } 
    public int Weekday { get { return _weekday; } set { _weekday = value; } } 
    public Institution Institution { get { return _institution; } set { _institution = value; } } 
} 

機構映射類代碼:

public class Institution 
{ 
    private int _id = 0; 
    private string _caption = null; 
    private string _addressLine = null; 
    private string _phone = null; 
    private ICollection<WorkHours> _workTime = null; 

    public Institution(){} 
    public int Id { get { return _id; } internal set { _id = value; } } 
    public string Caption { get { return _caption; } set { _caption = value; } } 
    public string AddressLine { get { return _addressLine; } set { _addressLine = value; } } 
    public string Phone { get { return _phone; } set { _phone = value; } } 
    public ICollection<WorkHours> WorkTime { get { return _workTime; } set { _workTime = value; } } 
} 

正如你所看到的我已經設置在Institutions類中,但是當我選擇時,我只能得到WorkHours的一條記錄,而應該有7.它會生成好的sql並選擇必要的記錄,同時在管理工作室中執行它。我試過用袋子代替套裝,但是我得到了同樣的記錄7次。也許有人知道問題出在哪裏?


好吧,我改變了我的映射一點點:

<class name="Business.DomainObjects.Institutions.Institutions, Business.DomainObjects" 
    table="Institutions" lazy="false" mutable="false" > 
<id name="Id" access="field.camelcase-underscore" column="InstitutionID"> 
    <generator class="identity" /> 
</id> 

<property name="Caption" type="String" access="field.camelcase-underscore" column="Name" not-null="true"/> 
<property name="AddressLine" type="String" access="field.camelcase-underscore" column="AddressLine" not-null="true" /> 
<property name="Phone" type="String" access="field.camelcase-underscore" column="Phone" not-null="false" /> 
<set name="WorkTime" cascade="none" fetch="join"> 
    <key column="InstitutionID" not-null="true"/> 
    <one-to-many class="Business.DomainObjects.Institutions.WorkHours, Business.DomainObjects"/> 
</set> 

<class name="Business.DomainObjects.Institutions.WorkHours, Business.DomainObjects" 
table="WorkHours" lazy="false" mutable="false" > 
<id name="Id" access="field.camelcase-underscore" column="WorkHoursID"> 
    <generator class="identity" /> 
</id> 
<property name="Weekday" type="int" access="field.camelcase-underscore" column="Weekday" not-null="true" /> 
<property name="OpenFrom" type="DateTime" access="field.camelcase-underscore" column="OpenFrom" not-null="true" /> 
<property name="OpenTill" type="DateTime" access="field.camelcase-underscore" column="OpenTill" not-null="true" /> 
<property name="OpenTime" type="String" access="field.camelcase-underscore" column="OpenTime" not-null="true" /> 
<many-to-one name="Institution" class="Business.DomainObjects.Institutions.Institution, Business.DomainObjects" column="InstitutionID" /> 
</class> 

,但我仍然有同樣的問題。

+0

您是否將該機構映射到WorkHours對象?你能告訴我們db-table中的數據嗎? – Tobias

+0

我們是否也可以查看您的映射類代碼。 – Bobby

+0

我更新了我的問題。 – JNM

回答

0

我發現我的問題。我不得不改變我的WorkHours映射並添加此

<composite-id access="field.camelcase-underscore"> 
    <key-many-to-one 
     class="Business.DomainObjects.Institutions.Institution, Business.DomainObjects" 
     name="Institution" 
     column="InstitutionID" 
     access="field.camelcase-underscore" 
    /> 
    <key-property 
     name="Weekday" 
     type="int" 
     access="field.camelcase-underscore" 
     column="Weekday" 
    /> 
</composite-id> 

相反的:

<id name="Id" access="field.camelcase-underscore" column="WorkHoursID"> 
    <generator class="identity" /> 
</id> 
<property name="Weekday" type="int" access="field.camelcase-underscore" column="Weekday" not-null="true" /> 

現在的作品完美。

1

這看起來腥:

<key **column="ID"** not-null="true"/> 

應該映射到WorkHours表的外鍵列,因此大概就像InstitutionID

0

這些看起來不正確:

and

<many-to-one name="Institution" class="Business.DomainObjects.Institutions.Institution, Business.DomainObjects" column="ID" /> 

Column = XX在兩種情況下均指WorkHours表中的外鍵列。嘗試像列= InstitutionID

相關問題