-->
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.  [ 2 posts ] 
Author Message
 Post subject: Multiple Criteria Hibernate Query involving "and" "or"
PostPosted: Wed Dec 24, 2014 3:19 pm 
Newbie

Joined: Wed Dec 24, 2014 3:13 pm
Posts: 1
I am trying to query with multiple criteria:
1. I would like to use the the parameter, statementNumber, that is passed in the getCaseByStatemetnNumber(String statementNumber) { ... } method call
2. Using the "statementNumber", I am searching for the "caseStatus" that is either "open" or "closed-unpaid".

I have the code below but have not yet managed to get the query correct. I am writing to seek help. Thank you.

Code:
    ..... 
    ..... 
     
          public void populateCritieria(final Map<? extends Enum<?>, Object> searchCriteria, 
                final Criteria criteria) 
           { 
     
                if (searchCriteria.containsKey(CaseSearchType.statementNumber))   
                { 
                    criteria.add(Restrictions.eq("statementNumber", searchCriteria.get(CaseSearchType.statementNumber))); 
                }                 
                 
                if (searchCriteria.containsKey(CaseSearchType.caseStatus))   
                { 
                    criteria.add(Restrictions.eq("caseStatus", searchCriteria.get(CaseSearchType.caseStatus))); 
                }   
                 
           } 
             
           public Case getCaseByStatementNumber(String statementNumber) 
           { 
            Map<CaseSearchType,Object> searchCriteria = new HashMap<CaseSearchType,Object>(); 
            searchCriteria.put(CaseSearchType.statementNumber, statementNumber); 
            searchCriteria.put(CaseSearchType.caseStatus, "open"); 
            searchCriteria.put(CaseSearchType.caseStatus, "closed-unpaid"); 
            return search(searchCriteria,Case.class); 
           }         
...
... 


Top
 Profile  
 
 Post subject: Re: Multiple Criteria Hibernate Query involving "and" "or"
PostPosted: Mon Dec 29, 2014 3:21 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
You cannot put two objects with the same key into a map. But you can put "open" and "closed-unpaid" into a List<String> and put that list into the map.

And for your criteria you can use a Disjunction (org.hibernate.criterion.Disjunction), iterate over your list and add the items to that disjunction.


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