java - Order ResultList of @ManyToMany connection -
i have @manytomany connection temporary table.
table1 has 0..* table2 , table2 0..* table1.
when ask elements of table1 , serialize it. array of table1 containing array of table2. array of table1 sorted primary key. array table2 in table 1 randomly sorted.
how can sort array of table2 in table1 primary key?
code:
table1
@table(name = "person") @entity public class persondetail1 extends person implements serializable { private static final long serialversionuid = 1l; @manytomany(fetch = fetchtype.eager) @jointable(name = "person_item", joincolumns = {@joincolumn(name = "pers_fk", referencedcolumnname = "pers_pk")}, inversejoincolumns = {@joincolumn(name = "item_fk", referencedcolumnname = "item_pk")}) private set<item> items; }
table2
@entity public class item implements serializable { private static final long serialversionuid = 1l; @id @column(name = "item_pk") @generatedvalue(strategy = generationtype.identity) private int id; @manytomany(fetch = fetchtype.lazy) @jointable(name = "person_item", joincolumns = {@joincolumn(name = "item_fk", referencedcolumnname = "item_pk")}, inversejoincolumns = {@joincolumn(name = "pers_fk", referencedcolumnname = "pers_pk")}) private set<persondetail1> personen; }
get table1
@override public list<persondetail1> getpersonen() throws detailexception { typedquery<persondetail1> q = em.createquery("select x persondetail1 x", persondetail1.class); return q.getresultlist(); }
get serialized object
@get @path("/personen") @produces(mediatype.application_json) @override public response getpersonen(){ return response.status(response.status.ok).entity(persservice.getpersonen()).build(); }
if understood requirement correctly, want have collection
of items
in persondetail1
sorted.
you can either use orderby
annotation that, or ordercolumn
annotation.
orderby
can used dynamic ordering, meaning sorting occur on query results.
ordercolumn
can used persistent ordering - order of items stored in column of item
table. persistent ordering has quirks, needs go orderby
:
i have doubts ordering have effects on contents of regular set
. might want use sorted collection
implementation sortedset
.
@orderby("id asc") private set<item> items;
the default ordering asc
, not need write explicitly, did illustration purposes.
Comments
Post a Comment