nHibernate set只给出一个结果
本文关键字:一个 结果 set nHibernate | 更新日期: 2023-09-27 18:08:41
我必须映射类:
<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并选择必要的记录,同时在管理工作室中执行。我试过用袋子代替set,但我得到同样的记录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>
这看起来很可疑:
<key **column="ID"** not-null="true"/>
它应该映射到WorkHours表中的外键列所以可能像InstitutionID
这些看起来不对:
和
<many-to-one name="Institution" class="Business.DomainObjects.Institutions.Institution, Business.DomainObjects" column="ID" />
Column=XX在这两种情况下都是指WorkHours表中的外键列。比如Column=InstitutionID
我找到我的问题了。我必须更改我的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" />