jayanr wrote:
Hi
I am using spring with Hibernate.
I have a class hirachy. Base class - Person . Subclass is 'Employee'.
I am having a Table per hirachy mapping and the discrinminator column is Person_Code. If Person_ Code is 'EMP', the Employee instance need to be created otherwise, Person.
Now, I have a person Id and I am interested only if the person is an Employee.That is, I need a method which looks for an employe and returns the employee if exists, otherwise throw an exception or null.
So I have e a method in My EmployeeDao 'Employee findEmployeeByPersonId(Long personId)'
the hibernate Implementation :
--------------------------------
Employee findEmployeeByPersonId(Long personId){
return (Employee)getHibernateTemplate().get(Employee.class,personId);
}
Hbm mapping
-----------
?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="mypackage.Person" table="Person" discriminator-value ="PERSON">
<id name="partId" column="person_id" unsaved-value="0">
<generator class="increment" />
</id>
<discriminator type="string" formula="case when PERSON_CODE='EMP' then 'EMPLOYEE' else 'PERSON' end"/>
<property name="personName" column="person_Name" not-null="false"/>
<property name="personCode" column="person_Code" not-null="false"/>
<property name="personAge" column="person_Age" not-null="false"/>
<subclass name="mypackage.Employee" discriminator-value="EMPLOYEE">
<property name="empAttr" column="emp_Attr" not-null="false"/>
</subclass>
</class>
</hibernate-mapping>
But surprisingly when I pass a person Id who is not an Employee, this method return me an Employee Object.
Am I doing something wrong ?. Please advice.
jaynar,
This appears to be a bug in v3x.
I am currently putting together a test case to sumbit to the Hibernate JIRA.
You can get around this for now by using a Criteria object. It only works incorrectly with Session.get() and Session.load()
Preston