java - JPA (EclipseLink) list order -
java - JPA (EclipseLink) list order -
i'm having problem keeping order of list during persistence when using eclipselink. missing setting in eclipselink, or forgetting utilize annotation?
i have 2 classes looks this:
@entity @table(name = "sequence") public final class sequence { @id @generatedvalue(strategy = generationtype.identity) private long id; @onetomany(cascade = cascadetype.persist, mappedby = "sequence") private list<run> runs; // other fields // ... } @entity @table(name = "run") public final class run { @id @generatedvalue(strategy = generationtype.identity) private long id; @manytoone @joincolumn(name = "seq_id", nullable = false) private sequence sequence; @column(nullable = false) private string comment; // other fields // ... }
now, expect when add together instances of latter class former, order in list kept when persiting:
run = new run(); a.setcomment("a"); run b = new run(); b.setcomment("b"); list<run> runs = new linkedlist<run>(); runs.add(a); runs.add(b); sequence seq = new sequence(); seq.setruns(runs); persist(seq); // order in database should "a", "b"
however, when check database, it's not instance a gets lower identifier b; seems behave if list bucket.
how maintain order of list when using persistence? if instance a added before b, want maintain order in database entries results of sequential execution of tests , order must perserved.
thanks help!
if want maintain order, you'll need @ordercolumn annotation - default, collection saved "bag" semantic (no order involved). can added on onetomany or manytoone annotations.
something like:
@onetomany(cascade = cascadetype.persist, mappedby = "sequence") @ordercolumn private list<run> runs;
see http://docs.oracle.com/javaee/6/api/javax/persistence/ordercolumn.html info.
java jpa eclipselink
Comments
Post a Comment