Набор nHibernate дает только один результат - PullRequest
1 голос
/ 16 сентября 2011

Мне нужно сопоставить классы:

<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 и выбирает необходимые записи, выполняя его в Management Studio.Я пытался использовать сумку вместо набора, но я получаю одну и ту же запись 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 ]

1 голос
/ 16 сентября 2011

Это выглядит подозрительно:

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

Должно отображаться в столбец внешнего ключа в таблице WorkHours, поэтому, вероятно, что-то вроде InstitutionID

0 голосов
/ 20 сентября 2011

Я нашел свою проблему.Я должен был изменить свое отображение 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" />

Теперь оно отлично работает.

0 голосов
/ 16 сентября 2011

Они выглядят неправильно:

и

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

Столбец = XX в обоих случаях ссылаются на столбец внешнего ключа в таблице WorkHours.Попробуйте что-то вроде Column = InstitutionID

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...