I have a two tables in my database, a Company table with an ID and company name, and a CrossSell table with a companyId field, and several url strings. I've got them mapped so there are two classes mapped to that table: Company, and CompanyWithCrossSell, which inherits Company. I'm using two classes because Company is used frequently, but CompanyWithCrossSell is used seldom.
My problem is that when getting a list of companies (with Criteria query), I get every company twice, once as the Company class, and once as CompanyWithCrossSell. So, my question is, am I doing something wrong, or is this just a bad idea?
Classes:
Code:
public class IdValueTuple<TId, TVal>
{
public virtual TId Id { /* property get/set */ }
public virtual TVal Value { /* property get/set */ }
}
public class Company : IdValueTuple<int, string>
{
public override bool Equals(object obj)
{
Company other = obj as Company;
if (other == null) return false; // obj is null or not Company class
if (this.Id == 0 || other.Id == 0)
{
// one or both Id's are transient, so compare Value
return (this.Value == other.Value);
}
return (Id == other.Id);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
// also implements IComparable, ICloneable interfaces
}
public class CompanyWithCrossSell : Company
{
public virtual CrossSell CrossSell { /* property get/set */ }
}
public class CrossSell
{
public virtual int CompanyId { /* property get/set */ }
public virtual string Url1 { /* property get/set */ }
// ... and so on
}
Mapping documents:Code:
<class name="Company" table="company">
<id name="Id" column="id" type="int">
<generator class="sequence">
<param name="sequence">company_id</param>
</generator>
</id>
<property name="Value" column="company" />
</class>
<class name="CompanyWithCrossSell" table="company">
<id name="Id" column="id" type="int">
<generator class="sequence">
<param name="sequence">company_id</param>
</generator>
</id>
<property name="Value" column="company" />
<one-to-one name="CrossSell" class="CrossSell"/>
</class>
<class name="CrossSell" table="crossSell">
<id name="CompanyId" column="companyid" type="int">
<generator class="assigned"/>
</id>
<property name="Url1" column="url1" type="StringClob" />
<property name="Url2" column="url2" type="StringClob" />
<property name="Url3" column="url3" type="StringClob" />
</class>
Code between sessionFactory.openSession() and session.close():ICriteria search = session.CreateCriteria( typeof(Company) );
search.AddOrder( Order.Asc("Value") );
List<Company> list = (List<Company>)search.List<Company>();
NHibernate version: 1.2.0GA
Name and version of the database you are using:Oracle 10g
[/code]