我正在使用带有休眠反应的 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)));
}
任何建议表示赞赏。