Alex Rivera | Logout

Hibernate query gives same record multiple times

Asked 2012-05-12T15:02:46.597
23

I am working on hibernate in eclipse. I am executing simple 'From' query. Here is the code

  List list = sess1.createQuery("From Myview").list();
    System.out.println("Records Found :"+list.size());

    Iterator<Myview> i = list.iterator();

    while(i.hasNext())
    {
        Myview nS = i.next();
        System.out.println(nS.getFirstName()+" -- "+nS.getLastName()+" -- "+nS.getAddressLine1());
    }

The problem is the list.size() returns 11, which is right as i have 11 records in my table. But when i am in while loop, the same records shown multiple times and the loop termintes after 11th iteration. here is my output

enter image description here

here is what i want

enter image description here

Now you can see that in my output, record is displayed 11 times but the same record is repeated again and again. And what i need is the output displayed in the later image.

Kindly help me in this regard, as i am new to hibernate

Edit
Report

1 Answer

1

You should use the distinct keyword to filter the same result.

answered 2012-05-12T16:17:05.683

Your Answer