java - Cascade persist creates duplicate rows? -


i'm creating database entity object order, , assign multiple entities of type bookingcode.

problem: creates single order in db, fine. order has @onetoone orderdescription, occurs duplicate in database.

@entity public class bookingcode {     @id     private long id;      @manytoone(cascade = {cascadetype.merge, cascadetype.persist, cascadetype.refresh, cascadetype.detach})     private order order; }  @entity public class order {     @id     private long id;      private string ordername;      @onetoone(mappedby = "order", cascade = cascadetype.all, orphanremoval = true)     private orderdescription description; }  @entity public class orderdescription {     @id     private long id;      //for simplicity 1 text element; of course multiple fields in real life     private string text;      @onetoone     private order order; } 

test:

order order = new order(); order.setordername("test");  orderdescription d = new orderdescription("testdescr"); d.setorder(order); order.setdescription(d);  list<bookingcodes> codes = new arraylist<>();  bookingcode code = new bookingcode(); code.setorder(order); codes.add(order);  bookingcode code2 = new bookingcode(); code2.setorder(order); //using same offer entity! codes.add(order2);  codes = dao.save(codes); //crudrepository spring  dao.findone(codes.get(0).getid()); //this works, find order has 1 of orderdescriptions 

result: in database have two orderdescription entries, expect one, because reused same order object , assigned different bookingcode objects.

like:

table order_descrption: 1;"de";"testdescr";"123456" 2;"de";"testdescr";"123456" 

as order has @onetoone relation orderdescription , don't understand why select using findone() works correctly. because in database have 2 orderdescriptions map same order, order can have 1 of them.

persist order first , assign both bookingcode .


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -