hibernate - how to save parent with detached child

前端 未结 2 2080
难免孤独
难免孤独 2020-12-19 08:29

I am sending an object from UI. This object is going to be created with a reference to an existing child.

This is simple illustration for this relation.

<         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 09:22

    Just use the proxy for the child:

    parentEntity.setChild(entityManager.getReference(ChildEntity.class, childId));
    

    The point here is to use EntityManager.getReference:

    Get an instance, whose state may be lazily fetched.

    Hibernate will create the proxy containing only the id without going to the database.

提交回复
热议问题