nHibernate 设置只给出一个结果

发布于 2024-12-05 03:00:34 字数 5330 浏览 2 评论 0原文

我必须映射类:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-12-12 03:00:34

这看起来很可疑:

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

它应该映射到 WorkHours 表中的外键列,因此可能类似于InstitutionID

This looks fishy:

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

It should map to the foreign key column in the WorkHours table so probably something like InstitutionID

勿忘初心 2024-12-12 03:00:34

这些看起来不对:

 
 

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

Column=XX 在这两种情况下都引用 WorkHours 表中的外键列。尝试类似 Column=InstitutionID

These don't look right:

and

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

Column=XX in both cases refer to the foreign key column in the WorkHours table. Try something like Column=InstitutionID

家住魔仙堡 2024-12-12 03:00:34

我发现了我的问题。我必须更改我的 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" />

它现在工作正常。

I Found my problem. I had to change my WorkHours mapping and add this

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

Instead of:

<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" />

It works perfectly now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文