2011-09-16 87 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

相关问题