-->
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: Simple criteria query
PostPosted: Wed Jun 04, 2008 4:57 am 
Newbie

Joined: Wed Jun 04, 2008 4:17 am
Posts: 2
Hi,


I'm new with criteria search and I didn't find solution on net for my simple problem. (maybe I didn't search enough:))

The question is :
- I have two table eg. Item and Bid and they have field productype which is object too. How can I represent this sql statement with Criteria?

from ITEM i, BID b where i.producttype.name = b.producttype.name


and second question is :
How can I represent this dinamic sql with Criteria ?

Code:

stringQuery="ProductEntity list where"

if(distributors!=null && !distributors.isEmpty()){
   for(Warehouse warehouse:distributors){
      stringQuery.append(" list.distributors like '");
      stringQuery.append("%");
      stringQuery.append(warehouse.getId());
      stringQuery.append("%' or ");               
   }

...




Best regards,
mb


Top
 Profile  
 
 Post subject: Re: Simple criteria query
PostPosted: Wed Jun 04, 2008 11:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

mladen.babic wrote:
Hi,
from ITEM i, BID b where i.producttype.name = b.producttype.name


Have you tried something like this - you basically can join tables via the Criteria API using the following syntax:
Code:
session.createCriteria(Item.class)
   .createAlias("bids", "b")
   .add( Restriction.eq("producttype.name" = "b.producttype.name"));


Quote:
and second question is :
How can I represent this dinamic sql with Criteria ?

Code:

stringQuery="ProductEntity list where"

if(distributors!=null && !distributors.isEmpty()){
   for(Warehouse warehouse:distributors){
      stringQuery.append(" list.distributors like '");
      stringQuery.append("%");
      stringQuery.append(warehouse.getId());
      stringQuery.append("%' or ");               
   }



Not sure what you mean, but in case your problem is how to use a like query in the Criterion API have a look at Restrictions.like.

--Hardy


Top
 Profile  
 
 Post subject: Re: Simple criteria query
PostPosted: Thu Jun 05, 2008 3:55 am 
Newbie

Joined: Wed Jun 04, 2008 4:17 am
Posts: 2
Tanx for quick replay, Hardy



Quote:
from ITEM i, BID b where i.producttype.name = b.producttype.name

Have you tried something like this - you basically can join tables via the Criteria API using the following syntax:

session.createCriteria(Item.class)
.createAlias("bids", "b")
.add( Restriction.eq("producttype.name" = "b.producttype.name"));

Yes , I know that , but my problem is that two tables have not relation between , only has same object producttype. I think yours solution is only for tables whose have relations.

Best regards,
mb

P.S. Sorry for my bad English!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 12:18 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Why not just do a criteria query on the second object by creating an example object and initializing the property. Since there is no relationship between then, you won't be able to use a Java based association.

Here's a little tutorial on using the Criteria API that might help out:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.