hibernate - 如何使惰性关系保持在休眠状态下反应
问题描述
我正在使用带有休眠反应的 Quarkus,一旦我添加了 @OneToMany 注释,我就无法使我的实体持续存在。我向您展示我的所有代码:
实体一:树:
@Entity(name = "Tree")
@Table(name = "tree")
public class Tree{
@Id
@Column(length = 50, unique = true)
public String name;
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JsonManagedReference
private List<Fruit> treeFruits;
public void addFruits(Fruit fruit) {
treeFruits.add(fruit);
fruit.setOwner(this);
}
public void addFruits(List<Fruit> fruits) {
treeFruits.addAll(fruits);
fruits.stream().forEach(f->f.setOwner(this) );
}
// Getters and setters removed
实体二:水果:
@Entity
@Table(name = "fruit")
public class Fruit {
@Id
@Column(length = 50, unique = true)
public String name;
@Column(length = 200)
public String description;
@Column(length = 50)
public String family;
@Column(nullable = false)
public Boolean ripen = false;
@ManyToOne //(fetch = FetchType.LAZY)
@JoinColumn(name = "tree_name", referencedColumnName = "name")
@JsonBackReference
private Tree owner;
树库:
@ReactiveTransactional
public Uni<Tree> create(Tree tree) {
return sf.withTransaction((s, t) -> {s.persist(tree); return s.flush();})
.replaceWith(sf.withTransaction((s, t) -> s.find(Tree.class, tree.name)));
}
create 方法不会在数据库中保留任何树,因此所有对 session.find(...) 的调用都返回 null。在将 Tree 关系添加到水果之前,使用此方法在水果存储库中一切正常
public Uni<Fruit> create(Fruit fruit) {
return sf.withTransaction((s, t) -> s.persist(fruit))
.replaceWith(sf.withTransaction((s, t) -> s.find(Fruit.class, fruit.name)));
}
任何建议表示赞赏。
解决方案
推荐阅读
- ios - 我想在单击单元格时更改集合视图单元格的图像
- reactjs - 如何使用 Github Actions 保存构建文件(React)?
- java - 如何解决此计算器错误?我是初学者
- python - 并非所有字段都在 django admin 中正确显示
- python - 我如何将这些数字写入其中的列表,因为我尝试使用 for 循环但它不起作用并将其写入名为 list_of_numbers 的列表
- biginteger - 不调用大量函数
- firebase - Flutter Web NoSuchMethodError 上的 Firebase 火库。“文档”
- flutter - 为什么滚动直到最后都无法正常工作
- regex - 如何保留您在 Notepad++ 中替换的部分文本
- java - 打开与 Windows MSSQL 服务器的 JDBC 连接时,Ubuntu 上的 OpenJDK1.8 错误