-->
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.  [ 1 post ] 
Author Message
 Post subject: Criteria Query Set
PostPosted: Sun Sep 19, 2004 1:41 pm 
Newbie

Joined: Sat Jan 31, 2004 9:36 am
Posts: 13
Location: Zurich
Hello,

I think my question is simple I have this objects (simplified):

Code:
class A{
   private Set bSet; // Object of Type B
   ...
}

class B{
   ...
}


The user can select 0..n B's of all existing B's and he wants to have all A's which have min one of the selected B's.

So, I implemented it in this way:
Code:
    public A[] searchAs(B[] selectedBs)

        Long[]  selectedBsIds = new Long[selectedBs.length];
                   
        for (int i = 0; i < selectedBs.length; i++) {
            selectedBsIds[i] = selectedBs[i].getId();                   
        }
   
        Session session = getSession();
        Criteria aCriteria = session.createCriteria(A.class);                       
        Criteria bCriteria = aCriteria.createCriteria("bSet");               
        bCriteria.add(Expression.in("id", selectedBsIds));         

        aCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return (A[]) aCriteria.list().toArray(new A[0]);       
       
    }


But I don't like the for(){} and the handling with the id's. Is there a way to do it directly, like:

Code:
    public A[] searchAs(B[] selectedBs)
   
        Session session = getSession();
        Criteria aCriteria = session.createCriteria(A.class);                       
        Criteria bCriteria = aCriteria.createCriteria("bSet");               
        bCriteria.add(Expression.in(selectedBs)));         

        aCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return (A[]) aCriteria.list().toArray(new A[0]);       
       
    }


Thanks and Regards
Oliver


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.