-->
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.  [ 7 posts ] 
Author Message
 Post subject: HQL query returns instance of abstract superclass.
PostPosted: Mon Sep 12, 2005 7:22 pm 
Newbie

Joined: Mon Sep 12, 2005 6:32 pm
Posts: 4
I have a class hierachy using joined sublasses. I then try and use an HQL query to return a List of objects but the class returned is an instance of the abstract superclass of the object I expect to have returned.

The class of the returned object is shown in my debugger as EntityImpl, whereas I was expecting a IndividualImpl.
Debugger shows "[0]= EntityImpl$$EnhancerByCGLIB$$de451a6c "

The HQL query is
"FROM com.kazgroup.courtlink.domain.impl.common.EntityImpl entity, com.kazgroup.courtlink.domain.impl.proceeding.EntityProceedingImpl ep WHERE entity.entityId = ep.key.entityImpl.entityId AND ep.key.proceedingImpl.proceedingNumber = :p AND ep.key.proceedingRoleImpl.code = :role ORDER BY entity.displayName asc "

Hibernate version: 3.0.5

Mapping documents:
<?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="com.kazgroup.courtlink.domain.impl.common.EntityImpl"
table="ENTITY"
dynamic-update="true"
optimistic-lock="version"
>

<id
name="entityId"
column="ENTITY_ID"
type="java.lang.Long"
>
<generator class="sequence">
<param name="sequence">ENTITY_ID</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-EntityImpl.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<version
name="lockVersion"
column="LOCK_VERSION"
type="java.lang.Long"
/>

<property
name="entityTypeString"
type="java.lang.String"
update="true"
insert="true"
column="ENTITY_TYPE"
not-null="true"
unique="false"
/>

<property
name="entityStatus"
type="java.lang.String"
update="true"
insert="true"
column="ENTITY_STATUS"
unique="false"
/>

<property
name="preferredContactTypeString"
type="java.lang.String"
update="true"
insert="true"
column="PREFERRED_CONTACT_TYPE"
unique="false"
/>

<set
name="contacts"
lazy="true"
inverse="true"
cascade="all"
sort="unsorted"
>

<key
column="ENTITY_ID"
>
</key>

<one-to-many
class="com.kazgroup.courtlink.domain.impl.common.ContactImpl"
/>

</set>

<property
name="modifiedDate"
type="java.util.Date"
update="true"
insert="true"
column="MODIFIED_DATE"
not-null="true"
unique="false"
/>

<property
name="modifiedByUser"
type="java.lang.String"
update="true"
insert="true"
column="MODIFIED_BY_USER"
not-null="true"
unique="false"
/>

<!--
Display name.
This is added seperately because of bug or anomoly in XDoclet
when referencing joined subclasses with overridden methods.
-->
<property
name="displayName"
type="java.lang.String"
update="true"
insert="true"
column="DISPLAY_NAME"
unique="false"
/>

<joined-subclass
name="com.kazgroup.courtlink.domain.impl.document.DocumentRecipientImpl"
table="DOCUMENT_RECIPIENT"
>
<key
column="ENTITY_ID"
/>

<property
name="surname"
type="java.lang.String"
update="true"
insert="true"
column="SURNAME"
not-null="true"
unique="false"
/>

<property
name="givenName"
type="java.lang.String"
update="true"
insert="true"
column="GIVEN_NAME"
not-null="true"
unique="false"
/>

</joined-subclass>
<joined-subclass
name="com.kazgroup.courtlink.domain.impl.participant.IndividualImpl"
table="INDIVIDUAL"
>
<key
column="ENTITY_ID"
/>

<property
name="givenName"
type="java.lang.String"
update="true"
insert="true"
column="GIVEN_NAME"
unique="false"
/>

<property
name="surname"
type="java.lang.String"
update="true"
insert="true"
column="SURNAME"
not-null="true"
unique="false"
/>

</joined-subclass>
<joined-subclass
name="com.kazgroup.courtlink.domain.impl.participant.LegalPractitionerImpl"
table="LEGAL_PRACTITIONER"
>
<key
column="ENTITY_ID"
/>

<property
name="practitionersState"
type="java.lang.String"
update="true"
insert="true"
column="PRACTITIONERS_STATE"
unique="false"
/>

<property
name="pcn"
type="java.lang.String"
update="true"
insert="true"
column="PCN"
unique="false"
/>

<property
name="surname"
type="java.lang.String"
update="true"
insert="true"
column="SURNAME"
not-null="true"
unique="false"
/>

<property
name="givenName"
type="java.lang.String"
update="true"
insert="true"
column="GIVEN_NAME"
unique="false"
/>

<property
name="practitionersFirm"
type="java.lang.String"
update="true"
insert="true"
column="PRACTITIONERS_FIRM"
unique="false"
/>

<many-to-one
name="legalPractitionerTypeImpl"
class="com.kazgroup.courtlink.domain.impl.refdata.LegalPractitionerTypeImpl"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="LEGAL_PRACTITIONER_TYPE"
not-null="true"
/>

</joined-subclass>
<joined-subclass
name="com.kazgroup.courtlink.domain.impl.participant.OrganisationImpl"
table="ORGANISATION"
>
<key
column="ENTITY_ID"
/>

<property
name="organisationName"
type="java.lang.String"
update="true"
insert="true"
column="ORGANISATION_NAME"
not-null="true"
unique="false"
/>

<property
name="abn"
type="java.lang.String"
update="true"
insert="true"
column="ABN"
unique="false"
/>

<property
name="acn"
type="java.lang.String"
update="true"
insert="true"
column="ACN"
unique="false"
/>

<property
name="fui"
type="java.lang.String"
update="true"
insert="true"
column="FUI"
unique="false"
/>

<property
name="isFeeExempt"
type="java.lang.String"
update="true"
insert="true"
column="FEE_EXEMPT"
not-null="true"
unique="false"
/>

</joined-subclass>
<joined-subclass
name="com.kazgroup.courtlink.domain.impl.registry.ExternalAgencyImpl"
table="EXTERNAL_AGENCY"
>
<key
column="ENTITY_ID"
/>

<property
name="agencyType"
type="java.lang.String"
update="true"
insert="true"
column="EXTERNAL_AGENCY_TYPE"
not-null="true"
unique="false"
/>

<property
name="agencyName"
type="java.lang.String"
update="true"
insert="true"
column="AGENCY_NAME"
not-null="true"
unique="false"
/>

<property
name="contactName"
type="java.lang.String"
update="true"
insert="true"
column="CONTACT_NAME"
not-null="false"
unique="false"
/>

<property
name="abn"
type="java.lang.String"
update="true"
insert="true"
column="ABN"
unique="false"
/>

<property
name="acn"
type="java.lang.String"
update="true"
insert="true"
column="ACN"
unique="false"
/>

<property
name="feeExempt"
type="java.lang.String"
update="true"
insert="true"
column="FEE_EXEMPT"
not-null="true"
unique="false"
/>

<property
name="effectiveFrom"
type="java.util.Date"
update="true"
insert="true"
column="EFFECTIVE_FROM"
not-null="true"
unique="false"
/>

<property
name="effectiveTo"
type="java.util.Date"
update="true"
insert="true"
column="EFFECTIVE_TO"
not-null="true"
unique="false"
/>

</joined-subclass>
<joined-subclass
name="com.kazgroup.courtlink.domain.impl.registry.OrderMakerImpl"
table="ORDER_MAKER"
>
<key
column="ENTITY_ID"
/>

<property
name="givenName"
type="java.lang.String"
update="true"
insert="true"
column="GIVEN_NAME"
not-null="true"
unique="false"
/>

<property
name="surname"
type="java.lang.String"
update="true"
insert="true"
column="SURNAME"
not-null="true"
unique="false"
/>

<property
name="specialityArea"
type="java.lang.String"
update="true"
insert="true"
column="SPECIALITY_AREA"
not-null="true"
unique="false"
/>

<property
name="preferredPrefix"
type="java.lang.String"
update="true"
insert="true"
column="PREFERRED_PREFIX"
not-null="false"
unique="false"
/>

<property
name="preferredSuffix"
type="java.lang.String"
update="true"
insert="true"
column="PREFERRED_SUFFIX"
not-null="false"
unique="false"
/>

<property
name="eforumName"
type="java.lang.String"
update="true"
insert="true"
column="EFORUM_NAME"
not-null="false"
unique="false"
/>

<property
name="comments"
type="java.lang.String"
update="true"
insert="true"
column="COMMENTS"
not-null="false"
unique="false"
/>

<property
name="seniority"
type="java.lang.Long"
update="true"
insert="true"
column="SENIORITY"
not-null="true"
unique="false"
/>

<property
name="orderMakerTypeString"
type="java.lang.String"
update="true"
insert="true"
column="ORDER_MAKER_TYPE"
not-null="true"
unique="false"
/>

<set
name="orderMakerAppointments"
lazy="true"
inverse="true"
cascade="all"
sort="unsorted"
>

<key
column="ENTITY_ID"
>
</key>

<one-to-many
class="com.kazgroup.courtlink.domain.impl.registry.OrderMakerAppointmentImpl"
/>

</set>

</joined-subclass>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs: N/A

Name and version of the database you are using: Oracle 10g

The generated SQL (show_sql=true):
select entityimpl0_.ENTITY_ID as ENTITY1_0_, entityproc1_.PROCEEDING_NUMBER as PROCEEDING1_1_, entityproc1_.ENTITY_ID as ENTITY2_1_, entityproc1_.PROCEEDING_ROLE as PROCEEDING3_1_, entityimpl0_.LOCK_VERSION as LOCK2_12_0_, entityimpl0_.ENTITY_TYPE as ENTITY3_12_0_, entityimpl0_.ENTITY_STATUS as ENTITY4_12_0_, entityimpl0_.PREFERRED_CONTACT_TYPE as PREFERRED5_12_0_, entityimpl0_.MODIFIED_DATE as MODIFIED6_12_0_, entityimpl0_.MODIFIED_BY_USER as MODIFIED7_12_0_, entityimpl0_.DISPLAY_NAME as DISPLAY8_12_0_, entityimpl0_1_.SURNAME as SURNAME13_0_, entityimpl0_1_.GIVEN_NAME as GIVEN3_13_0_, entityimpl0_2_.GIVEN_NAME as GIVEN2_14_0_, entityimpl0_2_.SURNAME as SURNAME14_0_, entityimpl0_3_.PRACTITIONERS_STATE as PRACTITI2_15_0_, entityimpl0_3_.PCN as PCN15_0_, entityimpl0_3_.SURNAME as SURNAME15_0_, entityimpl0_3_.GIVEN_NAME as GIVEN5_15_0_, entityimpl0_3_.PRACTITIONERS_FIRM as PRACTITI6_15_0_, entityimpl0_3_.LEGAL_PRACTITIONER_TYPE as LEGAL7_15_0_, entityimpl0_4_.ORGANISATION_NAME as ORGANISA2_16_0_, entityimpl0_4_.ABN as ABN16_0_, entityimpl0_4_.ACN as ACN16_0_, entityimpl0_4_.FUI as FUI16_0_, entityimpl0_4_.FEE_EXEMPT as FEE6_16_0_, entityimpl0_5_.EXTERNAL_AGENCY_TYPE as EXTERNAL2_17_0_, entityimpl0_5_.AGENCY_NAME as AGENCY3_17_0_, entityimpl0_5_.CONTACT_NAME as CONTACT4_17_0_, entityimpl0_5_.ABN as ABN17_0_, entityimpl0_5_.ACN as ACN17_0_, entityimpl0_5_.FEE_EXEMPT as FEE7_17_0_, entityimpl0_5_.EFFECTIVE_FROM as EFFECTIVE8_17_0_, entityimpl0_5_.EFFECTIVE_TO as EFFECTIVE9_17_0_, entityimpl0_6_.GIVEN_NAME as GIVEN2_18_0_, entityimpl0_6_.SURNAME as SURNAME18_0_, entityimpl0_6_.SPECIALITY_AREA as SPECIALITY4_18_0_, entityimpl0_6_.PREFERRED_PREFIX as PREFERRED5_18_0_, entityimpl0_6_.PREFERRED_SUFFIX as PREFERRED6_18_0_, entityimpl0_6_.EFORUM_NAME as EFORUM7_18_0_, entityimpl0_6_.COMMENTS as COMMENTS18_0_, entityimpl0_6_.SENIORITY as SENIORITY18_0_, entityimpl0_6_.ORDER_MAKER_TYPE as ORDER10_18_0_, case when entityimpl0_1_.ENTITY_ID is not null then 1 when entityimpl0_2_.ENTITY_ID is not null then 2 when entityimpl0_3_.ENTITY_ID is not null then 3 when entityimpl0_4_.ENTITY_ID is not null then 4 when entityimpl0_5_.ENTITY_ID is not null then 5 when entityimpl0_6_.ENTITY_ID is not null then 6 when entityimpl0_.ENTITY_ID is not null then 0 end as clazz_0_, entityproc1_.LOCK_VERSION as LOCK4_83_1_, entityproc1_.IS_PRIMARY as IS5_83_1_, entityproc1_.MODIFIED_DATE as MODIFIED6_83_1_, entityproc1_.MODIFIED_BY_USER as MODIFIED7_83_1_ from ENTITY entityimpl0_ left outer join DOCUMENT_RECIPIENT entityimpl0_1_ on entityimpl0_.ENTITY_ID=entityimpl0_1_.ENTITY_ID left outer join INDIVIDUAL entityimpl0_2_ on entityimpl0_.ENTITY_ID=entityimpl0_2_.ENTITY_ID left outer join LEGAL_PRACTITIONER entityimpl0_3_ on entityimpl0_.ENTITY_ID=entityimpl0_3_.ENTITY_ID left outer join ORGANISATION entityimpl0_4_ on entityimpl0_.ENTITY_ID=entityimpl0_4_.ENTITY_ID left outer join EXTERNAL_AGENCY entityimpl0_5_ on entityimpl0_.ENTITY_ID=entityimpl0_5_.ENTITY_ID left outer join ORDER_MAKER entityimpl0_6_ on entityimpl0_.ENTITY_ID=entityimpl0_6_.ENTITY_ID, ENTITY_PROCEEDING entityproc1_, ENTITY entityimpl2_, PROCEEDING proceeding3_, PROCEEDING_ROLE proceeding4_ where entityproc1_.PROCEEDING_ROLE=proceeding4_.PROCEEDING_ROLE and entityproc1_.PROCEEDING_NUMBER=proceeding3_.PROCEEDING_NUMBER and entityproc1_.ENTITY_ID=entityimpl2_.ENTITY_ID and entityimpl0_.ENTITY_ID=entityimpl2_.ENTITY_ID and proceeding3_.PROCEEDING_NUMBER=? and proceeding4_.PROCEEDING_ROLE=? order by entityimpl0_.DISPLAY_NAME asc


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: Re: HQL query returns instance of abstract superclass.
PostPosted: Mon Sep 12, 2005 7:58 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
TrevorCampbell wrote:
an instance of the abstract superclass of the object I expect to have returned.


You can't have an instance of an abstract class.

TrevorCampbell wrote:
I was expecting a IndividualImpl.


IndividualImpl isn't in the HQL, so H isn't going to return it.


Top
 Profile  
 
 Post subject: Re: HQL query returns instance of abstract superclass.
PostPosted: Mon Sep 12, 2005 8:28 pm 
Newbie

Joined: Mon Sep 12, 2005 6:32 pm
Posts: 4
dennisbyrne wrote:
TrevorCampbell wrote:
an instance of the abstract superclass of the object I expect to have returned.

You can't have an instance of an abstract class.

Obviously, but hibernate doesn't seem to understand this fundamental java rule and uses GCLIB to create one.

dennisbyrne wrote:
TrevorCampbell wrote:
I was expecting a IndividualImpl.

IndividualImpl isn't in the HQL, so H isn't going to return it.

I thought that was the whole point of persisting a class hiearchy as joined subclasses and also why hibernate includes all the child tables in the actual SQL Query.


Top
 Profile  
 
 Post subject: Re: HQL query returns instance of abstract superclass.
PostPosted: Mon Sep 12, 2005 10:39 pm 
Newbie

Joined: Mon Sep 12, 2005 6:32 pm
Posts: 4
A partial solution.

It appears that if lazy="true" (the default) is specified in the mapping then the returned entity is a proxy that does not "know" which class it represents and so if you try to find what type of object was returned using instanceof then you will fail.

If you use lazy="false" then the correct objects are returned in the query.

I think this is really an error as the lazy parameter is only an optimization feature and should not break code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 5:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
instanceof use is more or less a bell that should sounds to you as a OO breaking.

And this "optimization" is absolutly critical to avoid to load the whole DB in memory at your first request

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 7:02 pm 
Newbie

Joined: Mon Sep 12, 2005 6:32 pm
Posts: 4
emmanuel wrote:
instanceof use is more or less a bell that should sounds to you as a OO breaking.

And this "optimization" is absolutly critical to avoid to load the whole DB in memory at your first request

Yes instanceof is one of those "bad" things, but maybe you can help.

We are only using this because we have a complex three level hierachy where the middle class (only an interface in reality) has no persistent properties, but represents a real business classification. Using polymorphic queries on the interface means our returned objects do not sort correctly (multiple query problem), but by doing queries against the top level of our class hierachy results in a single query which then sorts the children correctly but may include extra objects (only rarely and a very few in our business case). We are then using instanceof to exclude those that are from the wrong side of the class hierarchy tree.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 7:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
either use lazy=false at the class level or use the no-proxy feature of Hibernate 3.1 but both will load objects to know the instanceof type. Other solution is to load the needed objects using an HQL query to load the appropriate ones.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.