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'..