0

I am firing a query off to Hibernate which returns a list of 25 elements. I then iterate over the list to print out each element. The problem is that it is printing out the first element 25 times, not iterating through the elements.

Here the code for the first class AnalAction:

package secondary.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.Session;
import secondary.controller.AnalManager;
import secondary.controller.LocationManager;
import secondary.model.Anal_01;
import secondary.model.LocationMaster;

import com.opensymphony.xwork2.ActionSupport;

public class AnalAction extends ActionSupport implements ServletRequestAware {
private List<LocationMaster> loclist;
    //private List<CustomerMaster> custList;
    //private ArrayList<String> bgroup;
    private ArrayList<String> Location;


public AnalAction()
    {
        linkcontroller=new AnalManager();
        linkcontroller2=new LocationManager();
        //custController=new CustomerMasterManager();

    }


String target="";
    public String execute()
    {
        try{
loclist=linkcontroller2.locList();  //calling from below class LocationManager
        System.out.println("loclist.."+loclist.size());
Location=new ArrayList<String>();
        Iterator<LocationMaster> iter = loclist.iterator();



 while(iter.hasNext())      
   {            
      String  str=iter.next().getLoc_name();
      System.out.println("location   name..:"+str);
      Location.add(str);
      target="Entry";
   }        
System.out.println("location..."+Location);

Here is LocationManager:

package secondary.controller;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import secondary.model.LocationMaster;
import secondary.util.HibernateUtil;


public class LocationManager extends HibernateUtil {

public LocationManager(){};
@SuppressWarnings("unchecked")
public List<LocationMaster> locList()
{
    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tran=null;
    List<LocationMaster> loc=null;
    try
    {
        tran=session.beginTransaction();
        loc=(List<LocationMaster>) session.createQuery("from LocationMaster    order by loc_code ").list();
        if(loc.size()>0)
        {
            System.out.println("size.....j"+loc.size());

            tran.commit();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return loc;
}

}

Mike G
  • 746
  • 5
  • 19
Amol
  • 21
  • 1
  • 8
  • Could you please show us some code? How are you adding it to the Arraylist? – tobias86 Apr 04 '11 at 07:12
  • i just added code plz see it.. – Amol Apr 04 '11 at 09:24
  • What's in the LocationMaster table? Do multiple entries have the same loc_code? Since you're not grouping by the loc_code, it could be that you're just seeing the first 25 entries that all actually share the same loc_code, right? – Mikko Wilkman Apr 04 '11 at 13:08
  • no multiple entry have not same loc_code...there is only one loc_code for each location – Amol Apr 04 '11 at 13:20
  • Amol: when you checking this loc.size() what is the size coming and check the size of list also at action level..just post the results.also enable hibernate show SQL property whcih will help you to see what query hibernate is executing – Umesh Awasthi Apr 04 '11 at 13:20
  • ya i checked it it returning size 25 elements – Amol Apr 04 '11 at 13:21
  • you mean 25 elements count both at you LocationManager() method and at action class? if this is the case show the code of the Action class and also the place where you are fetching the records from the list? – Umesh Awasthi Apr 04 '11 at 13:26
  • i have already bulleted that action class code where i fetching list...see carefully i stucked there – Amol Apr 04 '11 at 13:38
  • Where do you see the ArrayList containing the bad data? When you return from the locList() method? When you transfer the contents to the new ArrayList in your action? In your view layer? – Steven Benitez Apr 04 '11 at 16:29
  • I'm being a bit thick but what exactly is the problem...Are you getting an exception? is so what? – Gareth Davis Apr 05 '11 at 07:48
  • not an exception but when i m tryimg to iterate arraylist loc and on printing it will print same 1st element 25 times... the size of arraylist is 25...list contains 25 diff. location(city) names...but it going to print 1st element 25 times ..plz see it – Amol Apr 05 '11 at 08:40
  • Why don't you add your actual question to the question-body. Its kinda hard to find out the problem by checking all the comments and hoping there's some explanation in there. – Nanne Apr 05 '11 at 09:32
  • Does your "while(iter.hasNext())" block display what you expect and it is only the jsp that displays incorrectly? There are two distinct parts to your problem 1) getting the data from the DB 2) Using struts2 to display the data. If you know you are getting good data into your list, then we shouldn't have to see the details of 1. Vice versa as well. Also why not give complete code examples? – Quaternion Apr 05 '11 at 11:32
  • Interesting class name(s)... Also, that first code snippet seems to be missing some closing brackets. – Mike G Mar 26 '12 at 23:51

2 Answers2

0

Put a breakpoint on if(loc.size()>0) and inspect loc to ensure that it contains what you expect i.e. 25 different entries.

If this isn't as you expect then turn on SQL logging to ensure that the SQL you expect to be executed is.

log4j.logger.org.hibernate.SQL=TRACE
Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
0

Take a look at your LocationMaster hibernate mapping. loc contains 1st element 25 times because your mapped primary key is not unique.

Fred
  • 4,846
  • 1
  • 23
  • 21