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