-->
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.  [ 2 posts ] 
Author Message
 Post subject: Loading olny superclass
PostPosted: Thu Feb 03, 2005 8:24 am 
Newbie

Joined: Thu Feb 03, 2005 8:14 am
Posts: 1
I have class A, and then classes B, C, D,... all of which extend class A.

I need only data that is in class A (say I need only ids). I make a simple hibernate query: from A where id = 100

Is there a way to make hibernate not check all the classes that extend class A to establish which concrete class does the resulting row belong to? Cause that results in a big query linking all the tables that extend class A and checking where the id is. And in this case I know I will only need the data from class A, so it would be nice, if I could prevent hibernate from trying to get anything else.


btw:
I found the same/similar question posted several times, but there is no satisfying answer:

http://forum.hibernate.org/viewtopic.php?t=935155
http://forum.hibernate.org/viewtopic.php?t=937721
http://forum.hibernate.org/viewtopic.php?t=930208

Hibernate version: Hibernate 2.1.7


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 11:29 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Look under the select clause: http://www.hibernate.org/hib_docs/refer ... l-examples
- e.g.: select cat.name from eg.DomesticCat cat
You can do the same for your class A, and retreive only the attributes that are part of class A. That way hibernate will not link all the tables to establish which class this object is really of.

Wanted solution introduces conflict with Hibernate's principle - you would eventually have two objects of different class instance with same id - which one is right?

Probably the only solution is that you make a new class DefaultA which is a superclass of A (maybe A then becomes empty class). Then set DefaultA's polymorphism to 'explicit'. When trying to load only A's you will actually load DeafultA's. The rest of B, C, ... will have a normal expected polymorphic behaviour.

>>Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a "lightweight" class that contains a subset of the table columns).

That will ofcourse requre new mapping for DeafultA (duplicated only A's).
And query then changes to 'from DefaultA where ...'.

Or you can run hbm2java on below mapping (get duplicated class DefaultA and A).

Here what your mapping should look like:
<hibernate-mapping>
<class
name="eg.DefaultA"
table="aaa"
dynamic-update="false"
dynamic-insert="false"
polymorphism="explicit"
>
<id
name="id"
column="aaa_id"
type="int"
unsaved-value="0"
>
<generator class="native">
</generator>
</id>
<property
name="ident"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="ident"
length="127"
not-null="true"
/>
</class>
<class
name="eg.A"
table="aaa"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="aaa_id"
type="int"
unsaved-value="0"
>
<generator class="native">
</generator>
</id>
<property
name="ident"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="ident"
length="127"
not-null="true"
/>
<joined-subclass
name="B"
table="bbb"
dynamic-update="false"
dynamic-insert="false"
>
<key
column="bbb_id"
/>
<property
name="protocolId"
type="java.lang.Integer"
update="true"
insert="true"
access="property"
column="protocol_id"
length="10"
/>
</joined-subclass>
</class>
</hibernate-mapping>


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