How do I to use Criteria query on "class" property with custom discriminator-values?
I've tried to find this from the docs, forums and google but found no clear answer. I tried various ways but none works:
Code:
s.createCriteria( Person.class )
.add( Restrictions.eq( "class", Employee.class.getName()) )
.list();
s.createCriteria( Person.class )
.add( Restrictions.eq( "class", "Employee") )
.list();
s.createCriteria( Person.class )
.add( Restrictions.eq( "class", Employee.class) )
.list();
HQL can do this without any problem. I must be missing something very basic here. I played around with the CriteriaQueryList and from the debugging log, it seems anything I passed is sent straight to the SQL. This is what I added to the test:
Code:
public void testRestrictionOnClassProperty() {
Session s = openSession();
Transaction t = s.beginTransaction();
Employee mark = new Employee();
mark.setName("Mark");
mark.setTitle("internal sales");
mark.setSex('M');
mark.setAddress("buckhead");
mark.setZip("30305");
mark.setCountry("USA");
Customer joe = new Customer();
joe.setName("Joe");
joe.setAddress("San Francisco");
joe.setZip("XXXXX");
joe.setCountry("USA");
joe.setComments("Very demanding");
joe.setSex('M');
joe.setSalesperson(mark);
Person yomomma = new Person();
yomomma.setName("mum");
yomomma.setSex('F');
s.save(yomomma);
s.save(mark);
s.save(joe);
// tried Employee.class, Employee.class.getName()
List results = s.createCriteria( Person.class )
.add( Restrictions.eq( "class", "Employee") )
.list();
assertEquals(1, results.size());
t.rollback();
s.close();
}
(I'm using v3.0.5, with join/Person.hbm.xml mapping file from the hibernate tests).