0

I have this code I am persisting like that:

for (int i = 0; i < listofplusieurdrapage.size(); i++) {
    persist(listofplusieurdrapage.get(i));
}

I have two values in

litsofplusieurdrapage => litsofplusieurdrapage.get(0) = 1
=>litsofplusieurdrapage.get(1) = 2

but when i check the database I found them in this order:

  • 2
  • 1

and when I add just a system.out.println to the code I have the good order

for (int i = 0; i < listofplusieurdrapage.size(); i++) {
    System.out.println(" Persist : " + listofplusieurdrapage.get(i));
    persist(listofplusieurdrapage.get(i));
}
  • 1
  • 2

do you have an explication of the problem ? and how I can keep the good order with out adding the system.out.println to my code ?

public void persist(Object object) {
    em.persist(object); 
} 
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
  • what do you mean by 'when i check the database', how do you access the database ? what is the type of 'listofplusieurdrapage' ? – Kartoch Apr 22 '14 at 08:43
  • While do you not use an explicit order for records in the db (select ... order by) you will get them in their "natural" order. Propably they have a unique id column. JPA will retrieve by this id in ascend order. I suppose when you "look" into the DB the program to visualize the records (phpmyadmin ?) will order by the id in descend order. – PeterMmm Apr 22 '14 at 08:56
  • i have the access to Mysql and i show the table...yes ofcourse i order them by ID and they have false order.. did i need to commit the persist before moving to another persist and how ?? – user3169880 Apr 22 '14 at 09:24

1 Answers1

0

There is no ordering in tables, see for example this question and other links there.

If you need an order on the listofplusieurdrapage, you must specify the ordering, see for example this question.

Community
  • 1
  • 1
Ishtar
  • 11,542
  • 1
  • 25
  • 31