I am new to hibernate and doing a simple join query.
Here are the details of my class
Is there any other good way to retrieve data by joining.
Employee class
public class Employee
{
#region Private
private int _empnum;
private int _age;
private Name _name;
//private EmpDependent _dep;
private IList<EmpDependent> _dependents = new List<EmpDependent>();
//private IList<EmpDependent> _dependents;
#endregion
#region Constructor
public Employee()
{
_name = new Name();
// _dep = new EmpDependent();
}
#endregion
#region Public properties
public virtual int EmpNum
{
get { return _empnum; }
set { _empnum = value; }
}
public virtual int Age
{
get { return _age; }
set { _age = value; }
}
public virtual string EmpName
{
get { return _name.CompleteName; }
}
public virtual Name Name
{
get { return _name; }
set { _name = value; }
}
//public virtual EmpDependent EmpDependent
//{
// get { return _dep; }
// set { _dep = value; }
//}
public virtual void AddDependent(EmpDependent d)
{
if (_dependents.Contains(d)) return;
_dependents.Add(d);
}
public virtual IList<EmpDependent> Dependents
{
get { return _dependents; }
set { _dependents = value; }
//return PersistanceRegistry.GetInstance().PersonFactory.
}
#endregion
}
Employee dependent
public class EmpDependent
{
#region Private
private int _id;
// private int _empnum;
private string _fname;
private string _lname;
private string _address;
private Employee _employee;
#endregion
#region Constructor
public EmpDependent(string fname)
{
_fname = fname;
}
public EmpDependent()
{
}
#endregion
public virtual int ID
{
get { return _id; }
set { _id = value; }
}
public virtual string Fname
{
get { return _fname; }
set { _fname = value; }
}
public virtual string Lname
{
get { return _lname; }
set { _lname = value; }
}
public virtual string Address
{
get { return _address; }
set { _address = value; }
}
//public virtual int EmpNum
//{
// get { return _empnum; }
// set { _empnum = value; }
//}
public virtual Employee Employee
{
get { return _employee; }
set { _employee = value; }
}
}
Hibernate version:1.2.0
Mapping documents:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name ="DomainModel.Employee,DomainModel" table="Employee" lazy ="false">
<id name ="EmpNum" type ="int">
<generator class="native" />
</id>
<property name="Age" column ="age" type="int" />
<component name ="Name" class="DomainModel.Name,DomainModel">
<property name ="FirstName" column ="FirstName" type ="string" length ="50" />
<property name ="LastName" column ="LastName" type ="string" length ="50" />
</component>
<list name ="Dependents" inverse="true" lazy="false" table ="Emp_Relations" cascade="all" >
<key column ="EmpNum" />
<index column ="R_ID" />
<one-to-many class="DomainModel.EmpDependent,DomainModel" />
</list>
</class>
<class name="DomainModel.EmpDependent,DomainModel" table ="Emp_Relations" dynamic-update ="true" lazy="false" >
<id name="ID" type ="int" column ="R_ID">
<generator class="native"/>
</id>
<property name="Fname" type ="string" column ="R_Fname" />
<property name="Lname" type ="string" column ="R_LName" />
<property name="Address" type ="string" column ="R_Address" />
<many-to-one name="Employee" column ="EmpNum" class="DomainModel.Employee,DomainModel" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
string strQuery = "";
strQuery = "select emp.LastName, dep.Fname, dep.Lname from Employee as emp,emp.Dependents as dep where emp.EmpNum =45 ";
return NHibernateHTTP.CurrentSession.CreateQuery(strQuery).List<Employee>();
Full stack trace of any exception that occurs:
Test method UnitTest.EmployeeRepositoryTest.FindByLastNameTest threw exception: NHibernate.QueryException: could not resolve property: LastName of: DomainModel.Employee [select emp.LastName, dep.Fname, dep.Lname from DomainModel.Employee as emp,emp.Dependents as dep where emp.EmpNum =45 ].
at NHibernate.Persister.Entity.AbstractPropertyMapping.ThrowPropertyException(String propertyName)
at NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName)
at NHibernate.Hql.Classic.PathExpressionParser.get_PropertyType()
at NHibernate.Hql.Classic.PathExpressionParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.Classic.SelectPathExpressionParser.End(QueryTranslator q)
at NHibernate.Hql.Classic.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.Classic.SelectParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.Classic.ClauseParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.Classic.ClauseParser.End(QueryTranslator q)
at NHibernate.Hql.Classic.PreprocessingParser.End(QueryTranslator q)
at NHibernate.Hql.Classic.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.Classic.QueryTranslator.Compile()
at NHibernate.Hql.Classic.QueryTranslator.Compile(IDictionary replacements, Boolean scalar)
at NHibernate.Impl.SessionFactoryImpl.GetQuery(String queryString, Boolean shallow, IDictionary enabledFilters)
at NHibernate.Impl.SessionImpl.GetQueries(String query, Boolean scalar)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters, IList results)
at NHibernate.Impl.SessionImpl.Find[T](String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List[T]()
at Persistance.EmployeeRepository.FindByLastName(String Name) in C:\Documents and Settings\sharr02\My Documents\Visual Studio 2005\Projects\NHTest\Persistance\EmployeeRepository.cs:line 35
at UnitTest.EmployeeRepositoryTest.FindByLastNameTest() in C:\Documents and Settings\sharr02\My Documents\Visual Studio 2005\Projects\Hybernate.Experiment.Web\UnitTest\EmployeeRepositoryTest.cs:line 96
Name and version of the database you are using:
SQL Server 2000
The generated SQL (show_sql=true):
select emp.LastName, dep.Fname, dep.Lname from DomainModel.Employee as emp,emp.Dependents as dep where emp.EmpNum =45
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
[/b]
|