[Sleep mode] Merge temporary object with children

I have problems with merging. My update method works as follows:

void update(Parent parent) {
    evict(parent);
    merge(parent);
}

My classes:

Parent {

  Long id;

  List<Children> childrens;

  @OneToMany(targetEntity =ChildrenImpl.class, fetch=FetchType.LAZY)
  @JoinColumn(name="PARENT")
  @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.ALL)
  List<Children> getChildrens(){...}

  @Id
  Long getId() {...}

}



Children{

  Parent parent;

  @ManyToOne(targetEntity = ParentImpl.class, fetch = FetchType.LAZY)
  @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.ALL)
  @JoinColumn(name = "PARENT", nullable = false)
  Parent getParent(){...}

}

When I create a new Parent object (transitional) and add new children and try to update (extrude and merge), then the logs show me this after a hibernation session:

INSERT PARENT //everythings here is ok.

INSERT CHILDREN // but without parent id(id=null)

The order is good, but the children do not have a parent insert identifier. Everything works fine when Parent is stored in the database, then children always have a good identifier.

Any ideas what I should do to get the identifier from a temporary object (from persisted in order).

Relationship KZ.

+3
source share
1 answer

, .

, ? , . , . , , , , .

+2

Source: https://habr.com/ru/post/1776977/


All Articles