-->
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.  [ 4 posts ] 
Author Message
 Post subject: Querying according to subclass type
PostPosted: Mon Nov 01, 2010 11:29 am 
Newbie

Joined: Mon Nov 01, 2010 11:11 am
Posts: 2
Location: Rome - Italy
Hi all, this is my first post/topic here, and my English may not be so perfect.
This is the problem: I have 5 classes:
PianoDiStudi, which has a member of type PianoDiStudiState (abstract), NonApprovato,Approvato and NonEsaminato, of type PianoDiStudiState.
I must execute a query which retrieves only PianoDiStudi objects with a PianoDiStudiState of NonEsaminato concrete type.
I have tried with this code:
Code:
public List<T> getByExample(Object o){
     Session sessione=null;
     List<T> risultato=null;
     try{
        sessione=HibernateUtil.getSession();
        risultato=(List<T>)sessione.createCriteria(this.model)
           .add(Example.create(o)).list();
     }
     catch(HibernateException he){
        he.printStackTrace();
        sessione.close();
     }
     finally{
        sessione.close();
     }
     return risultato;
  }

This is part of a generic DAO class, model represents the type of the Object to retrieve. This doesn't work, the result list is empty. Any suggestions?

EDIT: tried also with:
Code:
risultato=(List<T>)sessione.createCriteria(this.model)
           .add(Restrictions.eq("stato",pds.getStato())).list();

"stato" is the field of type PianoDiStudiState, and pds is a PianoDiStudiObject, with the stato set to NonEsaminato.
It retrieves all the objects, seems that it retrieves objects according to the static type (PianoDiStudiState), not the dynamic type (NonEsaminato), which is what I want.


Top
 Profile  
 
 Post subject: Re: Querying according to subclass type
PostPosted: Tue Nov 02, 2010 2:06 pm 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
If I'm understanding this correctly, it sounds like you should be able to return a list of PianoDiStudi objects associated with a type of NonEsaminato by making the relationship bidirectional and creating a Criteria query as follows:

Code:
session.createCriteria(NonEsaminato.class)
  .setProjection(Projections.property("pianoDiStudi"))
  .list();


It's possible I'm misunderstanding this entirely...


Top
 Profile  
 
 Post subject: Re: Querying according to subclass type
PostPosted: Wed Nov 03, 2010 6:13 am 
Newbie

Joined: Mon Nov 01, 2010 11:11 am
Posts: 2
Location: Rome - Italy
burroinquieto wrote:
If I'm understanding this correctly, it sounds like you should be able to return a list of PianoDiStudi objects associated with a type of NonEsaminato by making the relationship bidirectional and creating a Criteria query as follows:

Code:
session.createCriteria(NonEsaminato.class)
  .setProjection(Projections.property("pianoDiStudi"))
  .list();


It's possible I'm misunderstanding this entirely...

Solved creating a Criteria on a Criteria (the second Criteria checks a field of stato, which is of PianoDiStudiState type to see wether it's a NonApprovato or something else)


Top
 Profile  
 
 Post subject: Re: Querying according to subclass type
PostPosted: Thu Mar 31, 2011 10:43 am 
Newbie

Joined: Thu Mar 24, 2011 2:22 pm
Posts: 2
Hey, Dottor P++

I'm having the same situation as you and I'd like to see an example code of how exactly you solved this problem.

Could you post your solution?

Thanx


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