embedding - JPA @EmbeddedId: How to update part of a composite primary key? -
i have many-to-many relationship link table has additional property. hence link table represented entity class , called composition
. primary key of composition
@embeddable
linking according entities, eg. 2 @manytoone
references.
it can happen user makes error when selecting either of 2 references , hence composite primary key must updated. due how jpa (hibernate) works of course create new row (insert) instead of update , old composition
still exist. end result being new row added instead of 1 being updated.
option 1:
the old composition
deleted before new 1 inserted require according method handling requires both old , new version. plus since updated version new entity optimistic locking not work , hence last update win.
option 2:
native query. query increments version column , includes version in clause. throw optimisticlockexception
if update count 0 (concurrent modification or deletion)
what better choice? "common approach" issue?
why not change primary key of composition
uid auto-generated? users change 2 references entities being joined without having delete/re-create composition
entity. optimistic locking maintained.
edit: example:
@entity @table(name = "composition") public class composition { @id @column(name = "id") private long id; // auto-generate using preferred method @manytoone(fetch = fetchtype.lazy, optional = false) @joincolumn( .... appropriate .... ) private firstentity firstentity; @manytoone(fetch = fetchtype.lazy, optional = false) @joincolumn( .... appropriate .... ) private secondentity secondentity; ....
Comments
Post a Comment