spring - java.util.Collections.swap(List<?> list, int i, int j) do nothing -


i have java spring mvc , 1 of models category

@entity @getter @setter @noargsconstructor @allargsconstructor @tostring public class category extends auditmodel {     @jsonview(datatablesoutput.view.class)     string name;     boolean active;     @column(columndefinition = "text")     string description;     @column(length = 70)     string metatitle;     @column(length = 160)     string metadescription;     string friendlyurl;     @onetomany     list<category> secondarycategories;     string image;     long position; } 

i try swap positions of 2 categories in secondarycategories list java.util.collections.swap(list list, int i, int j) nothing happens, no errors too.

@override public responseentity movecategoryupordown(long id, long parentid, string direction) {     if (id == parentid) {         return new responseentity<>("home category cat't moved",httpstatus.bad_request);     }     category category = categoryrepository.findone(id);     category parent = categoryrepository.findone(parentid);     list<category> secondarycategories = parent.getsecondarycategories();     if (direction.compareto("up") == 0) {         if (secondarycategories.indexof(category) <= 0)             return new responseentity<>(httpstatus.ok);         int = secondarycategories.indexof(category);         int j = secondarycategories.indexof(category) - 1; //            category previous = secondarycategories.get(j);         collections.swap(secondarycategories, i, j); //            categoryrepository.saveandflush(previous); //            categoryrepository.saveandflush(category);         categoryrepository.saveandflush(parent);     } ... etc .. 

i tried entitymanager detach objects before swaping no success. made own swap method wasn't able swap.

how can swap 2 categories in secondarycategories?

you need update position fields, or whatever sets ordering of entity. changing order of collection means nothing point of view of orm.


Comments