-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hql and Criteria queries work differently for "Any Mapp
PostPosted: Fri Sep 15, 2006 2:48 am 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
I am using version 3.2.0.c4

My issue is, for a field in class that is declared like

<any id-type="long" name="mSubject" meta-type="int">
<meta-value value="1" class="eg.Student"/>
<meta-value value="3" class="eg.Teacher"/>
<meta-value value="4" class="eg.Principal"/>
<column name="SUBJECT_TYPE" length="2"/>
<column name="SUBJECT_NO" length="12"/>
</any>

Suppose we want to query subject type teacher

in hql this works fine
".. where mSubject.class=3 ..."

however with criteria queries this doesnt work and you cant get any result. After serious debugging I see that Criteria queries require "Class names as Strings" instead of discriminator values. So instead of

criteria.add(Restrictions.eq("mSubject.class", new Integer(3)))

you must use

criteria.add(Restrictions.eq("mSubject.class", "eg.Teacher")) // not even class but its fully qualified name as String

So I dont know is this a design choice that made because of some restrictions or bug; but it seem weird to me.

Here is the code part that caused this thing.

org/hibernate/type/MetaType.java
Code:
71   public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
72   throws HibernateException, SQLException {
72      baseType.nullSafeSet(st, value==null ? null : keys.get(value), index, session);
74   }

keys is a map that holds class names to discriminator values; so when you send the discriminator value as key, it returns null and query is executed with parameter null.

Just by replacing
"keys.get(value)"
with
"value"
should fix this problem, but dont know if results with sth. else that is not wanted. I did not tested it.

thank you for interest. if this is already known sorry for wasting your time


Ahmet Yetgin

_________________
a y


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 3:58 am 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
Anyone has an idea?

Should I expect a fix or work with class names?

Thanx

_________________
a y


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 6:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - it makes perfect sense to not have to refer to mapping specific details in the java code.

This is how it is supposed to work.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 6:14 am 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
If so, instead of using String (representation of class name)

using class looks like more appropriate.like :

criteria.add(Restrictions.eq("mSubject.class", Teacher.class))

The thing confused me was different usage of queries. Yes criteria api is about objects. Is HQL any different? No. It should use class name instead of discriminator values then...

Whatever. Theres no problem here except that in reference documentation this must be mentioned about somewhere.

Thank you for your kind response

Ahmet

_________________
a y


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 6:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
from the reference doc about hql and .class:

from Cat cat where cat.class = DomesticCat

so it works fine in HQL too.

_________________
Max
Don't forget to rate


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