JNM
JNM

Reputation: 33

nHibernate set gives only one result

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.

Upvotes: 1

Views: 225

Answers (3)

JNM
JNM

Reputation: 33

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.

Upvotes: 0

Thilak Nathen
Thilak Nathen

Reputation: 1333

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

Upvotes: 0

TomMhC
TomMhC

Reputation: 133

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

Upvotes: 1

Related Questions