-->
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.  [ 12 posts ] 
Author Message
 Post subject: Explicit Polymorphism with not null discriminator
PostPosted: Thu Feb 26, 2004 12:58 pm 
Newbie

Joined: Thu Feb 26, 2004 12:27 pm
Posts: 4
Hello,

I am using the following mapping:

Code:
<class name="SuperClass" table="my_table" discriminator-value="null">
   <id name="id" column="id" type="long">
      <generator class="identity"/>
   </id>
   <discriminator column="fk" />
   
   <property name="name" />
   
   <subclass name="SubClass" discriminator-value="not null">
      <many-to-one name="fk" />
   </subclass>
</class>


Now I want to load all SuperClass members, but without the SubClasses.

I think the best way would be to use explicit polymorphism. Because this is not supported on subclasses I have tried using it on the superclass (as supposed in http://forum.hibernate.org/viewtopic.php?t=926622), but it seems not to work.

So I have tried to use the query
Code:
where class = SuperClass
but that fails with an Exception "subclass test not allowed for null or not null discriminator".

Any help appreciated.
Thanks.


Top
 Profile  
 
 Post subject: Re: Explicit Polymorphism with not null discriminator
PostPosted: Thu Feb 26, 2004 1:52 pm 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
tnagel wrote:
Code:
where class = SuperClass



I have looked into "Chapter 10. Hibernate Query Language":
Code:
from eg.Cat cat where cat.class = eg.DomesticCat

Try to specify object identifier before class, and don't forget to declare it with full.package.class.Name

I am not sure, but maybe it helps you.


Top
 Profile  
 
 Post subject: Re: Explicit Polymorphism with not null discriminator
PostPosted: Thu Feb 26, 2004 2:00 pm 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
tnagel wrote:
Code:
<class name="SuperClass" table="my_table" discriminator-value="null">
   <id name="id" column="id" type="long">
      <generator class="identity"/>
   </id>
   <discriminator column="fk" />
   
   <property name="name" />
   
   <subclass name="SubClass" discriminator-value="not null">
      <many-to-one name="fk" />
   </subclass>
</class>



And I see you use some kind of strange descriminator-value for SuperClass and SubClass, try the following mapping

Code:
<class name="SuperClass" table="my_table" discriminator-value="SuperClass">
   <id name="id" column="id" type="long">
      <generator class="identity"/>
   </id>
   <discriminator column="fk" type="string" />
   
   <property name="name" />
   
   <subclass name="SubClass" discriminator-value="SubClass">
      <many-to-one name="fk" />
   </subclass>
</class>


And look into "Chapter 16. Inheritance Mappings".

Best regards,
shl


Top
 Profile  
 
 Post subject: Re: Explicit Polymorphism with not null discriminator
PostPosted: Thu Feb 26, 2004 3:21 pm 
Newbie

Joined: Thu Feb 26, 2004 12:27 pm
Posts: 4
Thanks for the quick response.

shl wrote:
Try to specify object identifier before class, and don't forget to declare it with full.package.class.Name

I had written the query that way. Just simplified it for the forum. So that does not work either.


Quote:
And I see you use some kind of strange descriminator-value for SuperClass and SubClass

My strange discriminator values result from the schema I want to map. The subclass has all the superclass' properties and additionally a relationship. The "fk" element is a foreign key and also the discriminator. That's why I am using "null" and "not null" values.

I think that would be a clean approach, but apparently I am doing something wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2004 3:59 pm 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
I don't think that it is possible to combine foreign key with descriminator.
I see you use table-per-hierarchy inheritance mapping. If you had more than one subclass your
Code:
discriminator-value="not null"
would not work, because you need one descriminator-value per subclass. I think you need to introduce descriminator column into "my_table" relation, I don't see another way.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 7:49 am 
Newbie

Joined: Thu Feb 26, 2004 12:27 pm
Posts: 4
It is possible to use a foreign key as descriminator. Only the explicit polymorphism does not work right.

I have looked into the source (WhereParser) and the exception is thrown if the discriminator is null or not null. But I do not understand why it is not possible to build such a SQL query.

Instead of the running query "where my_table.fk = 'SuperClass'" (if the discriminator is a String) the query "where my_table.fk is null" should work, too.

Or am I misunderstanding something?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 1:08 pm 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
Did you figure out a good solution to this? explicit does not work for me either, but it says in the docs:
Quote:
Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself. Explicit polymorphism means that class instances will be returned only be queries that explicitly name that class and that queries that name the class will return only instances of subclasses mapped inside this <class> declaration as a <subclass> or <joined-subclass>. For most purposes the default, polymorphism="implicit", is appropriate. 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).


Which makes me wonder what exactly does explicit do? seems to behave the same as implicit.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 5:25 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
superclasses won't be returned

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 6:24 pm 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
Ok, so how would you make it so that subclasses won't be returned?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 8:27 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use the .class in HQL
from eg.Cat cat where cat.class = eg.DomesticCat

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 8:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Note that what you asked is a non sense.
A SubClass is basically also a Class.
A String is basically an Object

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 12:37 pm 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
emmanuel wrote:
Note that what you asked is a non sense.
A SubClass is basically also a Class.
A String is basically an Object


Thanks for the help, but definitely don't need the lesson. Maybe queries should just include everything that is an object too since everything is basically an object so everything must be the same. Ya, that would be just great. Ya maybe, and maybe i'm a chinese jet pilot. ;-)


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