我目前正在修补一个简单的 HTTP 资源。我的模型由带有多个“果实”的“树”组成。两者都继承自 PanacheEntity。
树:
@Entity
public class Tree extends PanacheEntity {
public String name;
@OneToMany(mappedBy = "tree", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
public List<Fruit> fruits = new ArrayList<>();
}
水果
@Entity
public class Fruit extends PanacheEntity {
public String name;
public String color;
public int cores;
@ManyToOne
@JsonbTransient
public Tree tree;
}
资源:
import io.quarkus.hibernate.orm.rest.data.panache.PanacheEntityResource;
public interface TreeResource extends PanacheEntityResource<Tree, Long> { }
这是我通过 Swagger 发送的 POST 请求
{
"name": "Apple Tree",
"fruits": [
{
"color": "green",
"cores": 3,
"name": "Apple2"
},
{
"color": "red",
"cores": 4,
"name": "Apple"
}
]
}
响应告诉我,为所有对象创建了 ID:
{
"id": 4,
"fruits": [
{
"id": 5,
"color": "green",
"cores": 3,
"name": "Apple2"
},
{
"id": 6,
"color": "red",
"cores": 4,
"name": "Apple"
}
],
"name": "Apple Tree"
}
但是当我在 /trees 下调用 Get all 时,我得到以下响应:
[
{
"id": 1,
"fruits": [],
"name": "Oak"
},
{
"id": 4,
"fruits": [],
"name": "Apple Tree"
}
]
水果总是空的。检查 postgres 数据库显示 Fruit 中的所有“tree_id”列都是空的。我很确定这是一个初学者的问题,但是在检查了多个示例后,我找不到我的代码有什么问题。