user1280073
user1280073

Reputation: 1

ArrayList displaying only the last value of index

public ArrayList getEmpInfo(int id) {

        ArrayList data = new ArrayList();
        loginPojo lp=new loginPojo();
        EmployeeInfoPojo emp_info = new EmployeeInfoPojo();

        Session session = null;
        SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();

        session = sessionfactory.openSession();

        String sql_query = "from loginPojo where id!=" + id;
        Query query = session.createQuery(sql_query);

        List<loginPojo> list = query.list();

        Iterator it = list.iterator();

        while (it.hasNext()) {
            lp = (loginPojo) it.next();
            emp_info.setName(lp.getName());

            System.out.println("Before "+emp_info.getName());
            data.add(emp_info);
            System.out.println("After "+emp_info.getName());
        }
        return data;
    }

This is the code for getting the information from the database using the hibernate framework.I tried to display the ArrayList in the main using the following code.

  public static void main(String args[]) {
    EmployeeInfoPojo emip = null;
    EmployeeInfo emi = new EmployeeInfo();
    ArrayList info = emi.getEmpInfo(102);

    Iterator it = info.iterator();

    while (it.hasNext()) {
        emip = (EmployeeInfoPojo) it.next();
        System.out.println(emip.getName());
    }
}

The output Expected is: John Jose Mark

But what am getting is: Mark Mark Mark

Can anyone find me what's wrong with the code????

Upvotes: 0

Views: 2284

Answers (5)

chan
chan

Reputation: 1

    public ArrayList getEmpInfo(int id) {

            ArrayList data = new ArrayList();
            loginPojo lp=new loginPojo();
            Session session = null;
            SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
            session = sessionfactory.openSession();
            String sql_query = "from loginPojo where id!=" + id;
            Query query = session.createQuery(sql_query);
            List<loginPojo> list = query.list();
            Iterator it = list.iterator();
            while (it.hasNext()) {
//you need to create new object or else it uses same object reference
       EmployeeInfoPojo emp_info = new EmployeeInfoPojo();
                lp = (loginPojo) it.next();
                emp_info.setName(lp.getName());`enter code here`
                System.out.println("Before "+emp_info.getName());
                data.add(emp_info);
                System.out.println("After "+emp_info.getName());
            }
            return data;
        }

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Briefly, you need to create a new EmployeeInfoPojo object within the while loop. Otherwise all you do is change the properties of a single object over and over and over again.

Upvotes: 0

kundan bora
kundan bora

Reputation: 3889

You just create single object of EmployeeInfoPojo in method getEmployeeInfo. You need to initialize ep_info each time you iterate list. iterator code should look like this -

     while (it.hasNext()) {
    lp = (loginPojo) it.next();
    emp_info = new EmployeeInfoPojo
    emp_info.setName(lp.getName());


    System.out.println("Before "+emp_info.getName());
    data.add(emp_info);
    System.out.println("After "+emp_info.getName());
}

In your previous code you just operate on single object that is why when you change this object old value will override and ArrayList adding same object each time while looping. Hence only last value is reflected in all objects(single object with three reference).

Upvotes: 0

Erica
Erica

Reputation: 2251

Your "emp_info" variable is being created outside the loop, which means that the one inside the loop is always the same instance. When you put that instance into a list, it's still the same instance. (Adding something to a list adds that instance, not a copy.)

Move this line:

EmployeeInfoPojo emp_info = new EmployeeInfoPojo();

immediately after this line:

lp = (loginPojo) it.next();

and you should find that your loop behaves more like you would expect it to.

Upvotes: 0

Shashank Kadne
Shashank Kadne

Reputation: 8101

I think you need to change the below logic...

 while (it.hasNext()) {
        lp = (loginPojo) it.next();
        emp_info.setName(lp.getName());


        System.out.println("Before "+emp_info.getName());
        data.add(emp_info);
        System.out.println("After "+emp_info.getName());
    }

to

while (it.hasNext()) {
            lp = (loginPojo) it.next();
            emp_info = new EmployeeInfoPojo();//create object here
            emp_info.setName(lp.getName());
            System.out.println("Before "+emp_info.getName());
            data.add(emp_info);
            System.out.println("After "+emp_info.getName());
        }

Because, as you are adding the same object to the ArrayList, its taking the value you last updated.

Upvotes: 2

Related Questions