我有两个实体,
class A { @OneToOne B b; }
class B { ... lots of properties and associations ... }
当我创建new A() 然后保存时,我只想设置b 的id。
所以 new A().setB(new B().setId(123))。
然后保存并让数据库保留它。
我真的不需要或不想先从数据库中获取整个 B 来填充 A 的实例。
我记得这曾经有效,但当我测试时却无效。
我也尝试过 Cascade All。
最佳答案
B b = (B) hibernateSession.byId(B.class).getReference(b.getId());
a.setB(b);
hibernateSession.load(...) // can also be used as it does the same.
JPA 等价物是:
entitymanager.getReference(B.class, id)
关于java - Hibernate/JPA,保存新实体,同时仅在@OneToOne 关联上设置 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45515783/