Hi,
I have a table that represent groups of codes like countries,cities
etc.
I need just the list of countries from this table.
To define this table in the hbm.xml file I use the composite-id like
this :
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="StudentsNH" namespace="StudentsNH.Domain">
<class name="Country" table="mz_kodim">
<composite-id >
<key-property name="countryGroupCode" column="mispar_tab" />
<key-property name="countryCode" column="kod" />
<key-property name="LangCode" column="kod_safa" />
</composite-id>
<property name="countryDesc" column="teur" />
</class>
</hibernate-mapping>
This is the country class :
public class Country
{
public virtual int countryGroupCode { get; set; }
public virtual int countryCode{get;set;}
public virtual int LangCode { get; set; }
public virtual string countryDesc {get; set;}
public override bool Equals(object obj)
{
return true;
}
public override int GetHashCode()
{
return this.GetHashCode();
}
}
Here I get the country name :
public string SelectCountryByID(int n)
{
try
{
using (ISession session =
NHibernateHelper.OpenSession())
{
var s = session.Get<Country>(new Country()
{
countryGroupCode =
16,
countryCode = n,
LangCode = 1
});
return ((Country)s).countryDesc;
}
}
catch (Exception ex)
{
return null;
}
}
This works fine.
Now is my problem.
I have a student class which contain all its details and his birth
country.
This students table in the database contain just the code value of the
country.
In the class I want to represent a country entity rather than the code
value.
Here the student class I called "Demographic" :
public class Demographic
{
public virtual string idNumber { get; set; }
public virtual string hebFirstName { get; set; }
public virtual string hebLastName { get; set; }
public virtual string engFirstName { get; set; }
public virtual string engLastName { get; set; }
public virtual DateTime dateBirth { get; set; }
public virtual Country birthCountry { get; set; }
public virtual string telNumber { get; set; }
public virtual string cellNumber { get; set; }
public virtual string streetName { get; set; }
public virtual int streetNum { get; set; }
public virtual int mikudNum { get; set; }
public virtual string email { get; set; }
}
Now, I don't know what to write in the hbm.xml file so I tried this :
hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="StudentsNH" namespace="StudentsNH.Domain">
<class name="Demographic" table="Olim1">
<id name="idNumber" column="mispar_zehut" >
<generator class="assigned" />
</id>
<property name="hebFirstName" column="shem_prati"/>
<property name="hebLastName" column="shem_mishpaha"/>
<property name="engFirstName" column="shem_prati_l"/>
<property name="engLastName" column="shem_mishpaha_l" />
<property name="dateBirth" column="tarich_leyda" />
<many-to-one name="birthCountry" column="kod" fetch="join"
class="Country" />
<property name="telNumber" column="telephone" />
<property name="cellNumber" column="telephone_nayad"/>
<property name="streetName" column="rehov_ktovet" />
<property name="streetNum" column="mispar_ktovet" />
<property name="mikudNum" column="mikud_ktovet" />
<property name="email" column="email" />
</class>
</hibernate-mapping>
I try this to get few students instances :
public Demographic SelectStudent(string userName, string pass)
{
try
{
using (ISession session =
NHibernateHelper.OpenSession())
{
var s =
session.CreateCriteria(typeof(Demographic)).Add(Expression.Conjunction()
.Add(Expression.Eq("birthCountry.countryGroupCode",
16))
.Add(Expression.Eq("birthCountry.countryCode",
9))
.Add(Expression.Eq("birthCountry.LangCode",
1)))
.SetMaxResults(5).List<Demographic>();
return new Demographic(); // just for compile
}
}
catch (Exception ex)
{
return null;
}
}
I get an exception when I get this row:
using (ISession session = NHibernateHelper.OpenSession())
The error message :
Foreign key (FK8DC307354C14518B:Olim1 [kod])) must have same number of
columns as the referenced primary key (mz_kodim [mispar_tab, kod,
kod_safa])
I guess it's related to the Demographic.hbm.xml file.
Can you help me please to solve this problem ?
I'm using NHibernate 2.1.2
Thanks,
David
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.