-->
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.  [ 8 posts ] 
Author Message
 Post subject: Filtering Object's set property with respect to a criteria
PostPosted: Tue Apr 10, 2007 5:57 am 
Newbie

Joined: Tue Apr 10, 2007 5:40 am
Posts: 2
Hi Everyone,

I have a class called User which in turn holds a Set<Address> property named 'addresses' (each user can have multiple addresses). Address is another entity related to User by userId. Now I wish to filter out the list of users with only those addresses which has city (property of Address class) named, 'Newyork'.
Can anyone please help me to show how to add a Criteria using addCriteria() for the same?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 9:05 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Code:
Criteria c = session.createCriteria(User.class)
                     .createAlias("addresses", "a")
                     .add(Restrictions.eq("a.city", "Newyork");


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 7:47 am 
Newbie

Joined: Wed Apr 25, 2007 5:46 am
Posts: 7
Hi Ananasi,

I am currently working in Hibernate. The above code does "AND" operation by default. Any idea of making it to be an OR operation instead of AND.

[code]
Criteria c = session.createCriteria(User.class)
.createAlias("addresses", "a")
.add(Restrictions.eq("a.city", "Newyork");
[/code]

here is it possible to add restrictions to the User class? I am working on a search operation with more than one criteria. Any idea to solve my issue?


With thanks,
Amutha[/img]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 8:06 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,

You can get detailed information at,
www.hibernate.org/hib_docs/v3/reference ... teria.html

session.createCriteria(User.class).
add(Restrictions.or(
Restrictions.eq("city", "NewYork"),
Restrictions.eq("city", "Dallas")
)
).list().size() ;



------------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 8:18 am 
Newbie

Joined: Wed Apr 25, 2007 5:46 am
Posts: 7
Hi pramod,

I have tried the way given in the hibernate doc. Since I have got associations, I need to perform the search with an or operation instead of and (by default). Below is my sql track for your reference.

[code]
Hibernate: select this_.COMPANY_ID as COMPANY1_35_1_, this_.STATUS as STATUS35_1
_, this_.COMPANY_NAME as COMPANY3_35_1_, this_.SEARCH_NAME as SEARCH4_35_1_, thi
s_.SORT_NAME as SORT5_35_1_, this_.PHONE_NUMBER as PHONE6_35_1_, this_.PHONE_EXT
ENSION as PHONE7_35_1_, this_.FAX_NUMBER as FAX8_35_1_, this_.FAX_EXTENSION as F
AX9_35_1_, this_.LANGUAGE_PREFERENCE as LANGUAGE10_35_1_, this_.SUPPLIER_EXCEPTI
ON as SUPPLIER11_35_1_, this_.CAGE_CD as CAGE12_35_1_, this_.DUNS as DUNS35_1_,
this_.ENTERED_BY_USER_ID as ENTERED14_35_1_, this_.ENTERED_DATE as ENTERED15_35_
1_, this_.UPDATED_BY_USER_ID as UPDATED16_35_1_, this_.UPDATED_DATE as UPDATED17
_35_1_, this_.COMPANY_TYPE as COMPANY18_35_1_, a1_.ADDRESS_ID as ADDRESS1_32_0_,
a1_.COMPANY_ID as COMPANY2_32_0_, a1_.COUNTRY_CODE as COUNTRY3_32_0_, a1_.MAIL_
STOP as MAIL4_32_0_, a1_.PO_BOX as PO5_32_0_, a1_.SUITE_APARTMENT as SUITE6_32_0
_, a1_.STREET as STREET32_0_, a1_.CITY as CITY32_0_, a1_.POSTAL_CODE as POSTAL9_
32_0_, a1_.ENTERED_BY_USER_ID as ENTERED10_32_0_, a1_.ENTERED_DATE as ENTERED11_
32_0_, a1_.UPDATED_BY_USER_ID as UPDATED12_32_0_, a1_.UPDATED_DATE as UPDATED13_
32_0_, a1_.STATE_ID as STATE14_32_0_ from EAN_COMPANY this_, EAN_COMPANY_ADDRESS
a1_ where this_.COMPANY_ID=a1_.COMPANY_ID and ([b]this_.SEARCH_NAME like ? or this
_.COMPANY_TYPE like ?) and a1_.CITY=? and a1_.COUNTRY_CODE=?[/b]
[/code]

My criteria entered by the user are highlighted in bold above. You can notice and being performed. I need an OR operation there. Any idea to proceed with this................

With thanks,
Amutha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 9:06 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Which 'and' do you want to be an 'or', there are two 'ands' in the section you specified. Do you want
Code:
(this_.SEARCH_NAME like ? or this
_.COMPANY_TYPE like ?) or a1_.CITY=? or a1_.COUNTRY_CODE=?
or
Code:
(this_.SEARCH_NAME like ? or this
_.COMPANY_TYPE like ?) or (a1_.CITY=? and a1_.COUNTRY_CODE=?)
or some variant thereof?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 12:29 am 
Newbie

Joined: Wed Apr 25, 2007 5:46 am
Posts: 7
Hi Ananasi,

Thanks for your reply. I would say my requirement to u. In my case CompanyAddress is associated with Company. My search criteria includes fields like companyname and type from Company and city,country from CompanyAddress. The search fields are not mandatory. The user may select any of these for his choice. My daoimplementation method is like below.

[code]
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();
Criteria crit = session.createCriteria(Company.class).add(Restrictions.or(Restrictions.like("searchName",compname),Restrictions.like("companyType", companyType)))
.createAlias("addresses", "a")
.add(Restrictions.eq("a.city", city))
.add(Restrictions.eq("a.countryCode", ctry));
List results = crit.list();
[/code]

For the above said way, I feel the second option which you said would match.

[code]
(this_.SEARCH_NAME like ? or this
_.COMPANY_TYPE like ?) or (a1_.CITY=? and a1_.COUNTRY_CODE=?)
[/code]

Is there any way to proceed with this? Any help would be greatly appreciated.

With thanks,
Amutha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 1:41 am 
Newbie

Joined: Wed Apr 25, 2007 5:46 am
Posts: 7
Hi all,

Can anyone help me out to solve my issue facing with createCriteria for
an "OR" operation.

With thanks,
Amutha


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