gpt4 book ai didi

java - Hibernate/JPA,保存新实体,同时仅在@OneToOne 关联上设置 id

转载 作者:搜寻专家 更新时间:2023-10-30 20:50:24 24 4
gpt4 key购买 nike

我有两个实体,

  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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com