-->
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.  [ 8 posts ] 
Author Message
 Post subject: Criteria API and polymorphic queries
PostPosted: Wed Feb 25, 2004 12:26 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 7:53 am
Posts: 27
I have the following [sub]class structure: A <- B <- C (where B is a superclass of C and a child class of A), with discriminator values 'a', 'b' and 'c' accordingly, and using explicit polymorphism. I'm trying to do queries which only return instances of the needed class, no subclasses. Using the query language, I have zero problems doing so, but trying to use the Criteria API showed some interesting results.

Sample code:
Code:
criteria = session.createCriteria(B.class);
criteria.add(Expression.eq("class", "b"));
criteria.list();


generates SQL having this where clause (watching from P6Spy):
Code:
where this.doctype='b' and this.doctype in ('c', 'b')

..where doctype of course is the name of the discriminator column. Ok, the above works, but looks dirty enough to let me ask whether I'm doing it the right way here..

Some history: first I tried to assign B.class to the criteria, which gave a classcastexception - the Hibernate source clearly showed it was trying to cast it to a string. Then I tried to provide the full class name, which got inserted directly in the SQL above, so I *guess* I'd need to specify the actual discriminator value there.. am I right?

thanks,
dyn

ps. trying to find a similar issue here on the forums, I've bumped into the following q&a: http://forum.hibernate.org/viewtopic.php?t=927708&highlight=criteria+polymorphic, and I really wonder how I can set explicit polimorphism to a subclass, as the DTD only specifies polymorphism=[explicit|implicit] to the 'class' tag, but not for 'subclass'..

_________________
.Digital.Yearning.for.Networked.Assassination.and.Xenocide


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 4:42 pm 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
I have done something like this...

User is the superclass to many classes.

User (parent)
--- Administrator (child)
--- AccountManager (child)
--- FinanceAdministrator (child)

Code:
StringBuffer sb = new StringBuffer(50);
sb.append("from com.blah.User user ");
sb.append("where user.class = com.blah.Administrator ");
sb.append("or user.class = com.blah.AccountManager ");
sb.append("or user.class = com.blah.FinanceAdministrator ");
sb.append("order by user.class, user.firstName");
Query query = session.createQuery(sb.toString());


Check out 10.7 in the docs... http://www.hibernate.org/hib_docs/reference/html/query-language.html#query-language-s5

Likewise, the special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.
from eg.Cat cat where cat.class = eg.DomesticCat


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2004 3:28 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 7:53 am
Posts: 27
Collins, that's correct so far.. but I'd like to reach the same effect using the new Criteria API, not standard hql.[/quote]

_________________
.Digital.Yearning.for.Networked.Assassination.and.Xenocide


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2004 7:03 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't think this is doable. Check JIRA on such subjects

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 7:08 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 7:53 am
Posts: 27
Couldn't find anything like that in JIRA. Do you guys plan to do it, may I put this as a feature request there?

_________________
.Digital.Yearning.for.Networked.Assassination.and.Xenocide


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 12:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes into JIRA

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 1:01 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 7:53 am
Posts: 27
HB-768

Thanks in advance!

_________________
.Digital.Yearning.for.Networked.Assassination.and.Xenocide


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:30 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 7:53 am
Posts: 27
Gavin's comment in JIRA:
Quote:
Ah this doesn't bother me. -1 to complexifying the codebase for this.


Ok, I understand, that's your decision ofc :) The one fact I don't understand is, that if you push the new (and lovely!) Criteria API that much, and as you're so proud how polymorphism work with Hibernate and if polymorphism with Criteria already works (see my original comment!), just doesn't generate the cleanest possible SQL, then what's the point about complexifying the codebase.. Maybe I should have posted to JIRA as a minor fix, not as an improvement, or feature?

Anyway, sorry if I felt pushy above, and keep up the good work with Hibernate! :)

dyn

_________________
.Digital.Yearning.for.Networked.Assassination.and.Xenocide


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