-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to use Criteria query on "class" property
PostPosted: Wed Jul 06, 2005 8:33 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
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).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 9:32 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
I made a typo above, CriteriaQueryList should be CriteriaQueryTest (in org.hibernate.test.criteria)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 2:18 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
Is this question not clear or it's a potential defect? Please at least tell me that's I'm dumb or something :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 1:27 am 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
I'm going to switch to begging mode now...Seriously, should I submit this as a defect or is it just a stupid user error?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 4:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
put it in jira

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 12:25 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I am assuming a class hierarchy here but I find that:

Code:
s.createCriteria( Employee.class )


usually works for me.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 4:36 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
but that's really not I want (like include/exclude any subclasses). Anyhow, after digging through the source code, I found this is currently not in the code base and it would take quite a bit of changes to implement this so I just work around this by using the discriminator directly (:hack)

I'll post a jira just in case. I'm also curious to know why criteria wasn't implemented on top of HQL but its own SQL translation (thus the functionality mismatched)


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