Hibernate Inverse attribute

前端 未结 4 1911
孤街浪徒
孤街浪徒 2021-02-04 17:38

I am creating a one-to-many relationship. so, i have a parent and a child. The cascade attribute is set to all.

I was wondering, if we consider the following piece of co

4条回答
  •  一向
    一向 (楼主)
    2021-02-04 18:05

    Case inverse = false:

    In this case, it is parent's responsibility to save-update child and its relationship. So in your example, child will be updated in database. There will be two sql queries: 1) Insert child. 2) Update child with foreign key of parent id.

    Case Inverse = true:

    In this case , it is child's responsibility to save-update itself. So in your code, child will be saved in database but foreign key of parent will be null. Only one sql query will be executed and that is of insert child. For updating parent's foreign key, you need to manually save child.

    Child child = new Child();
    child.setParent(parent);
    session.save(child);
    

    I think, answer of these cases explains answer of your third question.

    Hope this help.

提交回复
热议问题