-->
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.  [ 1 post ] 
Author Message
 Post subject: Polymorphism Query
PostPosted: Mon Nov 28, 2005 9:08 pm 
Newbie

Joined: Fri Nov 18, 2005 1:50 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1

Mapping documents:

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I have a question reguarding an issue that was posted back in January 2004. Below is a full posting but in it Gavin mentions that "Typecasting is not supported in 2.1 but is in hibernate 3, I think". I was hoping to know if indeed this particular problem is resolved in hibernate 3. The post below is exactly what I am seeing with hibernate 2.x with this type of query:
// again the same, 'location' is not found on child
Query c = s.createQuery("select parent from test.Parent parent " +
"left outer join parent.children as child " +
"where child.location='location1'");

Here is the posting I am refering too.

Parent
|-- id

Child
|-- id
|-- name

SubChild extends Child
|-- location


the relationships are as

<class name="test.Parent">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<bag name="children" lazy="false" cascade="all">
<key column="parent_id"/>
<one-to-many class="test.Child"/>
</bag>
</class>


<class name="test.Child" discriminator-value="super">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<discriminator column="subclass" type="string"/>
<property name="name"/>

<subclass name="test.SubChild" discriminator-value="subchild">
<property name="location" type="string"/>
</subclass>
</class>


now the ONLY way i can find all children of Parent which match a certain value for 'location' seems to be as follows (note that 'location' is not on the Child class but in the subclass SubChild)


Query c = s.createQuery("from test.Child where location='location1'");


and all of the following attempts (which i think are very valid usage patterns), do not work


// this blows as it tries to find the 'location' field on Child and not SubChild
Criteria c = s.createCriteria(Parent.class)
.createCriteria("children")
.add(Expression.eq("location", "location1"));


// same thing here, but will work if we us SubChild.class instead
Criteria c = s.createCriteria(Child.class)
.add(Expression.eq("location", "location1"));


// again the same, 'location' is not found on child
Query c = s.createQuery("select parent from test.Parent parent " +
"left outer join parent.children as child " +
"where child.location='location1'");


// this is just a step away from working, removing the alias 'child' works
Query c = s.createQuery("from test.Child child where child.location='location1'");



Eventhough this can be somehow made to work in HQL but the extensibility I need for various types of queries that are formed in my app, is only available through the criteria API.

There has to be way for each createCriteria(associatoinPath) to retrieve not just the associated class but also all of it's subclasses.

If anyone has already got around to hacking at CriteriaImpl please let us know. Gavin if you already have a prototype that's not yet part of the product, we'll be glad to use it :-)

thanks - for bearing with me till here
ravi
Post rating: This posting has not been rated so far.
gavin
Hibernate Team
Hibernate Team


Joined: 26 Aug 2003
Posts: 5094
Location: Melbourne, Australia
118 credits (What's this?)


View user's profile

PostPosted: Sun Jul 25, 2004 3:56 pm Post subject: Reply with quote

"Typecasts" are not supported in Hibernate 2.1.

This code *does* work in HIbernate3 branch, I think.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.