-->
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.  [ 7 posts ] 
Author Message
 Post subject: Too many rows being returned from a query
PostPosted: Sun May 23, 2010 11:18 pm 
Newbie

Joined: Fri Apr 30, 2010 5:17 pm
Posts: 13
Hello. I'm passing an ArrayList of ids to my query. It shouldn't be returning all the rows of the table if only some of the ids are in the list, but somehow all the rows is what I'm getting. Can anyone help me with this? Thanks in advance.

Code:
public static List<Applicant> getByJob(List<Employment> employment) {
      try {
         SessionFactory sessionFactory = new Configuration().configure()
               .buildSessionFactory();
         Session session = sessionFactory.openSession();
         Criteria c = session.createCriteria(Applicant.class);
         Iterator<Employment> iter = employment.iterator();
         ArrayList<Integer> idList = new ArrayList<Integer>();
         while (iter.hasNext()) {
            Employment next = (Employment) iter.next();
            idList.add(next.getPersonid());
         }
         for (int i = 0; i < idList.size() - 1; i++) {
            for (int j = i + 1; j < idList.size(); j++) {
               if (idList.get(j) == idList.get(i)) {
                  idList.remove(j);
               }
            }
         }
         System.out.println("idList.size = " + idList.size());
         Query query = session.createQuery("from Person p where p.id_prsn in (:ids)");
         query.setParameterList("ids", idList);

         List<Applicant> results = (List<Applicant>) c.list();
         if (!results.isEmpty()) {
            return results;
         } else {
            return null;
         }

      } catch (HibernateException e) {
         e.printStackTrace();
         return null;
      }
   }


Top
 Profile  
 
 Post subject: Re: Too many rows being returned from a query
PostPosted: Mon May 24, 2010 2:19 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Criteria c = session.createCriteria(Applicant.class);
....
Query query = session.createQuery("from Person p where p.id_prsn in (:ids)");
query.setParameterList("ids", idList);

List<Applicant> results = (List<Applicant>) c.list();


I don't understand your code.... You are creating 1 Criteria and 1 Query. The criteria will load all Applicant:s and the query will load all Person:s that has an id that is in the list.

But you never execute the query, only the criteria.


Top
 Profile  
 
 Post subject: Re: Too many rows being returned from a query
PostPosted: Mon May 24, 2010 12:33 pm 
Newbie

Joined: Fri Apr 30, 2010 5:17 pm
Posts: 13
Quote:
Quote:
Criteria c = session.createCriteria(Applicant.class);
....
Query query = session.createQuery("from Person p where p.id_prsn in (:ids)");
query.setParameterList("ids", idList);

List<Applicant> results = (List<Applicant>) c.list();


I don't understand your code.... You are creating 1 Criteria and 1 Query. The criteria will load all Applicant:s and the query will load all Person:s that has an id that is in the list.

But you never execute the query, only the criteria.


The Applicant class maps to the Person class in the database.

I checked the Query API. It has a getResultList() method, and it returns a List, but Eclipse seems to be insisting that I cast it like this:

Code:
((Object) query).getResultList();


How do I execute the query, get only the rows I'm looking for, and have the query return a List?


Top
 Profile  
 
 Post subject: Re: Too many rows being returned from a query
PostPosted: Mon May 24, 2010 1:20 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know which API you are referring to here... org.hibernate.Query has no getResultList() method, it has a list() method whichI think would return your filtered data:

Code:
Query query = session.createQuery("from Person p where p.id_prsn in (:ids)");
query.setParameterList("ids", idList);
List<Person> result = (List<Person>)query.list();


Top
 Profile  
 
 Post subject: Re: Too many rows being returned from a query
PostPosted: Mon May 24, 2010 2:06 pm 
Newbie

Joined: Fri Apr 30, 2010 5:17 pm
Posts: 13
Quote:
I don't know which API you are referring to here... org.hibernate.Query has no getResultList() method, it has a list() method whichI think would return your filtered data:


Ah, I see. I was using this API rather than the correct one: http://java.sun.com/javaee/5/docs/api/j ... Query.html

Having said that, the code is still giving me problems. I have no Person class in my code. I have an Applicant class that maps to Person in the database. As such, I have this in my code:

Code:
Criteria c = session.createCriteria(Applicant.class);
...
Query query = session.createQuery("from Person p where p.id_prsn in (:ids)");
query.setParameterList("ids", idList);
List<Applicant> results = (List<Applicant>) query.list();


I am getting this exception report. SearchByJob, referenced in the first exception, is a separate class that's referencing this method. Any advice?
Code:
java.lang.NullPointerException
   com.ats.action.SearchByJob.execute(SearchByJob.java:199)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   java.lang.reflect.Method.invoke(Unknown Source)
   com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
   com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
   com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
   com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
   org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
   com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
   com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
   com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
   com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
   org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
   org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)


Top
 Profile  
 
 Post subject: Re: Too many rows being returned from a query
PostPosted: Mon May 24, 2010 4:31 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I have no Person class in my code. I have an Applicant class


That would explain a lot of the confusion.... In HQL you don't use table names and column names. You need to use the mapped class names it's properties.

Code:
Query query = session.createQuery("from Applicant a where a.id_prsn in (:ids)");


...except that I don't know which property you have mapped the id_prsn column to, so you have to fix that yourself.


Top
 Profile  
 
 Post subject: Re: Too many rows being returned from a query
PostPosted: Mon May 24, 2010 5:39 pm 
Newbie

Joined: Fri Apr 30, 2010 5:17 pm
Posts: 13
Quote:
Code:
Query query = session.createQuery("from Applicant a where a.id_prsn in (:ids)");


...except that I don't know which property you have mapped the id_prsn column to, so you have to fix that yourself.


Okay, I included your change and fixed the property mapping, and now the code works. Thank you very much for your help!


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