Announcement Announcement Module
Collapse
No announcement yet.
Returns subclass even though it does not exist. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returns subclass even though it does not exist.

    Hi

    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.clas s,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.

  • #2
    Don't know why this is happening, but I don't see why you need formula in your descriminator.

    Why don't you simply use person_code column for your discriminator?

    Comment


    • #3
      This is an example I used to explane my situation, But in my Actual problem, I need to use formula.

      Comment


      • #4
        I also don't see anything which appears to be wrong. I guess it is a bug or at least something not that obvious. Best would be to call in the Hibernate folks... . (Hibernate.org forum or bug report).

        Comment


        • #5
          I'm not 100% sure, but I have had some similar problem, and it turned out in hibernate "get" doesn't support polymorphism. "Query" does, i.e., You'd have to write a hql to select by id.

          Comment


          • #6
            Originally posted by manifoldronin
            and it turned out in hibernate "get" doesn't support polymorphism.
            Hmmm ... find it hard to believe that such basic stuff wouldn't work. And I have no such experience.

            This is an example I used to explane my situation, But in my Actual problem, I need to use formula.
            Then write the real problem, since this one can be solved with plain disciminator stuff.

            Comment


            • #7
              Originally posted by alesj
              Originally posted by manifoldronin
              and it turned out in hibernate "get" doesn't support polymorphism.
              Hmmm ... find it hard to believe that such basic stuff wouldn't work. And I have no such experience.
              I managed to dig up my thread on hibernate forum. It wasn't exactly the same symptom, but I still think the answer is generic enough :
              http://forum.hibernate.org/viewtopic.php?t=936051

              Comment


              • #8
                Yep, that way it doesn't work, since IAccount isn't mapped.

                Why it works in a query? Because it checks for all mapped classes if some is assignable from the current qery class.
                See SessionFactoryImpl.getImplementors(String className).

                But if you had a mapped MyAccount (which would extend Account), you would actually get a MyAccount instance - only correct thing considering polymorphism.

                Comment

                Working...
                X