Sarah
Sarah

Reputation: 57

JPA, update object in list

i have an ArrayList of offers ,but the problem is if an item in my ArrayList gets updated , it always becomes the last item of my list .

Exemple: i have a list of offers, i go to the database, i change the name of the first offer , when i display the list with getOffers , i will see that the item i updated moved from first position to last position .

Upvotes: 1

Views: 373

Answers (1)

Emil Hotkowski
Emil Hotkowski

Reputation: 2343

Selecting data from database

If you ever do select something from database without any sorting nor ordering you cannot be sure about sequence in which you retrive data.

More info HERE

Best way would be to put some Order By in your query.

OneToMany/ ManyToMany ordering

If you get your data from any JPA relations like @OneToMany or @ManyToMany where your collection is mapped from some column, without ordering you cannot be sure to keep always the same order. You will get any order your database engine will give you. Same idea as in previous point.

To order data in this case, you can sort it or (which I prefer) order it with annotation @OrderBy or @OrderColumn.

On difference between Sorting and Ordering LINK

Upvotes: 1

Related Questions