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; } }
}
正如您所见,我在机构类中进行了设置,但是当我选择时,我只得到一条 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>
但我仍然有同样的问题。
I have to mapped classes:
<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>
and
<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>
Mapping class code:
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; } }
}
Institution mapping class code:
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; } }
}
As you see i have set in Institutions class, but when i select, i get only one record of WorkHours while there should be 7. It generates good sql and selects necessary records, while executing it in management studio. I Tried using bag instead of set, but i get same record 7 times. Maybe somebody knows where the problem could be?
ok, i changed my mappings a little bit:
<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>
But i still have the same problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来很可疑:
它应该映射到 WorkHours 表中的外键列,因此可能类似于InstitutionID
This looks fishy:
It should map to the foreign key column in the WorkHours table so probably something like InstitutionID
这些看起来不对:
和
Column=XX 在这两种情况下都引用 WorkHours 表中的外键列。尝试类似 Column=InstitutionID
These don't look right:
and
Column=XX in both cases refer to the foreign key column in the WorkHours table. Try something like Column=InstitutionID
我发现了我的问题。我必须更改我的 WorkHours 映射并添加以下内容
而不是:
它现在工作正常。
I Found my problem. I had to change my WorkHours mapping and add this
Instead of:
It works perfectly now.