-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria with variable size.
PostPosted: Wed Aug 02, 2006 10:08 am 
Newbie

Joined: Wed Dec 14, 2005 7:27 am
Posts: 18
Hi,

probably there is a lot of posts with the same problem that i have, but i can't find any example. My problem is how to build a query with the Criteria, but the values after WHERE have a variable size.

I want to know the following:

SELECT * FROM TABLE1 WHERE ID = 1, ID = 2, ...., ID = 5.
The ID is a primary key.

The values after WHERE have a variable size, and may alternate. Ie, ID can have the values 2,4,5 or 1,3,5, etc...

This values are in a java.util.List.

criteria.createCriteria(Table1.class)
.add(<How should i do it?>);

Thanks,
Pedro


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 10:21 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Code:
Criteria c = createCriteria(class);
for (Iterator iter...) {
if (condition) {
c.add(Restriction.eq(prop, value);
}
}

execute

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 10:27 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I guess you can use similar code to the following

Code:
List idList =  new ArrayList();
idList.add( new Integer( 1 ) );
idList.add( new Integer( 2 ) );
idList.add( new Integer( 3 ) );

Criteria crit = session.createCriteria( Table1.class, "one" );
crit.add( Restrictions.in( "one.ID", idList ) );
List results = crit.list();


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