user { set<book> _books; @manytomany(cascade={cascadetype.merge}) @jointable( name="bletag_label", joincolumns={@joincolumn(name="tag_id", referencedcolumnname="id")}, inversejoincolumns={@joincolumn(name="label_id", referencedcolumnname="id")}) set<book>getbooks(); } book { }
. if set cascadetype persist , try create user existing book (book1) this, unique constraint fired...
book book1 = new book("book1"); user.getbooks().add(book1);
. current solution the cascadetype merge , check user.getbooks() if book exist. if book not exist, need create (because not cascadetype.persist)
is right solution ? there better "automatic" solution available kind of use case ?
as long use book current persistence context, cascade persist should work fine, , persist new books. if book detached, first need merge or find in current persistence context.
Comments
Post a Comment