nHibernate set只给出一个结果

时间:2011-09-16 12:27:45

标签: c# asp.net nhibernate nhibernate-mapping set

我必须映射类:

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

如你所见,我已经在机构类中设置,但是当我选择时,我只得到一条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>

但我仍有同样的问题。

3 个答案:

答案 0 :(得分:1)

这看起来很可疑:

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

它应该映射到WorkHours表中的外键列,所以可能是InstitutionID

答案 1 :(得分:0)

这些看起来不正确:

    

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

在两种情况下,Column = XX都指向WorkHours表中的外键列。尝试使用Column = InstitutionID

之类的东西

答案 2 :(得分: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" />

现在效果很好。