-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Returns Subclass instance even though the objdos not exist
PostPosted: Thu Apr 28, 2005 3:04 pm 
Newbie

Joined: Thu Apr 28, 2005 3:01 pm
Posts: 6
Location: Phoenix
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.

_________________
Jayan Nair


Top
 Profile  
 
 Post subject: Re: Returns Subclass instance even though the objdos not exi
PostPosted: Thu Apr 28, 2005 4:59 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
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


Top
 Profile  
 
 Post subject: Re: Returns Subclass instance even though the objdos not exi
PostPosted: Thu Apr 28, 2005 5:15 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I have opened the following JIRA issue:

http://opensource.atlassian.com/project ... se/HHH-416


Top
 Profile  
 
 Post subject: Re: Returns Subclass instance even though the objdos not exi
PostPosted: Fri Apr 29, 2005 8:53 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
pksiv wrote:
I have opened the following JIRA issue:

http://opensource.atlassian.com/project ... se/HHH-416



As you will see if you monitor this issue in JIRA...

According to Gavin...
"This is not a bug and has been the behavior of every version of Hibernate ever. "

So I guess we'll have to live with this behavior.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.