本文介绍了Hibernate-通过ID添加成员实体实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类型为A的实体,以及一个引用类型为B的成员实体.我想在A实例与B的现有实例之间插入关系,而不必获取B.
I have an entity of type A with a referenced member entity of type B.I want to insert a relationship between an instance of A to an existing instance of B without having to fetch B.
有没有一种方法可以简单地通过ID来完成?像这样:
Is there a way to do it simply by ID? Something like:
mockB.id ="id_persisted_b";
mockB.id = "id_persisted_b";
instanceA.setB(mockB);
instanceA.setB(mockB);
谢谢.
推荐答案
如果您使用的是JPA,则可以使用EntityManager.getReference()
为其提供对象的代理,而无需获取所有字段,例如:
If you are using JPA you can use EntityManager.getReference()
to obatin a proxy of the object without fetching all the fields, e.g.:
B mockB = entityManager.getReference(B.class, "id_persisted_b");
instanceA.setB(mockB);
这篇关于Hibernate-通过ID添加成员实体实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!