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: Big problem (bug) - one-to-many relations in subclasses
PostPosted: Wed Mar 24, 2010 8:10 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I think there's a probable bug in Hibernate when it comes to one-to-many relationships concerning subclasses that share the same properties.

I am trying to model 3 beans - EducationFacility, Director and Teacher.

Director and Teacher extend from UserAccount

EducationFacility contains 1 director
EducationFacility contains multiple teachers
Director and Teacher have a link back to EducationFacility.

The bug is when Hibernate tries to pull out educationFacility.teachers, it also pulls out the Director as well, because they both have a link to EducationFacility. Hibernate is not properly guarding the "teachers" collection by filtering out Directors. Here is the exception:

Code:
org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object with id: 9 was not of the specified subclass: jobprep.domain.user.Teacher (loaded object was of wrong class class jobprep.domain.user.Director); nested exception is org.hibernate.WrongClassException: Object with id: 9 was not of the specified subclass: jobprep.domain.user.Teacher (loaded object was of wrong class class jobprep.domain.user.Director)
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:666)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:837)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:833)


The mapping is as follows:

Code:
    <class name="jobprep.domain.educationfacility.EducationFacility" table="education_facility">
        <id name="id" column="education_facility_id" type="long">
           <generator class="native" />
        </id>

        ....

        <many-to-one name="director" class="jobprep.domain.user.Director"
                     column="director_id" cascade="all" />
        <bag name="teachers" inverse="true" cascade="all-delete-orphan" lazy="true" order-by="username asc">
            <key column="education_facility_id" />
            <one-to-many class="jobprep.domain.user.Teacher" />
        </bag>

        ....

    </class>

    <class name="jobprep.domain.user.UserAccount" table="user_account" abstract="true">
        <id name="id" column="user_account_id" type="long">
            <generator class="native" />
        </id>
        <discriminator column="user_type" type="string"/>

        ....

        <subclass name="jobprep.domain.user.Teacher" discriminator-value="ROLE_TEACHER">
            <many-to-one name="educationFacility" class="jobprep.domain.educationfacility.EducationFacility"
                         column="education_facility_id" not-null="false"/>
            <bag name="students" inverse="true" cascade="all-delete-orphan"
                 lazy="true">
                <key column="teacher_id" />
                <one-to-many class="jobprep.domain.student.Student"/>
            </bag>
        </subclass>

        <subclass name="jobprep.domain.user.Director" discriminator-value="ROLE_DIRECTOR">
            <many-to-one name="educationFacility" class="jobprep.domain.educationfacility.EducationFacility"
                         column="education_facility_id" not-null="false"/>
        </subclass>

        ....
    </class>



Top
 Profile  
 
 Post subject: Re: Big problem (bug) - one-to-many relations in subclasses
PostPosted: Wed Mar 24, 2010 10:50 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Will changing one of the educationFacility links inside of Teacher/Director to a different name/column fix this? Like, Teacher could keep education_facility_id and Director could have a different column called owning_education_facility_id?

I'm not sure why I need to do that for... but I see no other way to fix this. And I'm thinking Hibernate will probably complain if I do that.


Top
 Profile  
 
 Post subject: Re: Big problem (bug) - one-to-many relations in subclasses
PostPosted: Thu Mar 25, 2010 12:39 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
This code actually fixes it:

Code:
        ...

        <subclass name="jobprep.domain.user.Director" discriminator-value="ROLE_DIRECTOR">
            <many-to-one name="educationFacility" class="jobprep.domain.educationfacility.EducationFacility"
                         column="owning_education_facility_id" not-null="false"/>
        </subclass>

        ...


This works because hibernate's SQL probably pulls out all the UserAccount's that have education_facility_id=?... and since I changed the column name, Hibernate will only pull out Teacher subclasses of UserAccount.

Likewise, if I ever wanted to get all directors (if I ever changed it to be more than one), Hibernate's sql would only fetch the Directors.

But this can't be right... this is so cludgy and bad. I'll work with it for now, but I think there is a problem with Hibernate that needs to be addressed here. When educationFacility.teachers is ever used, it shouldn't pull out Directors in the first place in the sql... but it is.


Top
 Profile  
 
 Post subject: Re: Big problem (bug) - one-to-many relations in subclasses
PostPosted: Thu Mar 25, 2010 4:11 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Another possible workaround may be to include a where condition in the bag mapping. Something like this:

Code:
<bag name="teachers" .... where="user_type='ROLE_TEACHER'">


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.