one to many - JPA+EclipseLink - Move entities in a @OneToMany relationship -
my implementation of onetomany relationsship in description:
http://en.wikibooks.org/wiki/java_persistence/onetomany
entity group:
@entity public class group { @onetomany(cascade = { cascadetype.all }, orphanremoval = true) private list<unit> units = new arraylist<unit>(); public void addunit(unit unit) { this.units.add(unit); if (unit.getgroup() != this) { unit.setgroup(this); } }
entity unit:
@entity public class unit { @manytoone(cascade = cascadetype.persist) private group group; public void setgroup(group group) { this.group = group; if (group != null && !group.getgooditems().contains(this)) { group.getgooditems().add(this); } } }
now want move 1 unit between 2 groups with:
unit.setgroup(group2);
after operation can see in database in relation table group_unit unit related group1 , group2.
how can move unit between 2 groups correctly?
you should add mappedby in annotation:
@onetomany(cascade = { cascadetype.all }, orphanremoval = true, mappedby="group") private list<unit> units = new arraylist<unit>();
with mappedby not need join_table anymore.
Comments
Post a Comment