Reputation: 399
I am trying to test 1:m mapping on quarkus with gradle. Extensions used:
quarkus-hibernate-orm-panache'
quarkus-spring-web'
POSTMAN:
POST on http://localhost:8080/tree with request:
{
"name":"mango",
"fruits": [
{"name":"a",
"color":"red"
},
{"name":"b",
"color":"yellow"
}
]
}
Now when doing GET on http://localhost:8080/tree getting fruits as empty. Why it is coming as empty not able to find.
[
{
"treeId": 1,
"name": "mango",
"fruits": []
}
]
I have used following 1 controller class and 2 entity class.
@RestController
public class TreeController {
@PostMapping("/tree")
@Transactional
public void addTree(Tree tree) {
Tree.persist(tree);
}
@GetMapping("/tree")
public List<Tree> getTree() {
return Tree.listAll();
}
}
@Entity
public class Tree extends PanacheEntityBase{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long treeId;
public String name;
@OneToMany(mappedBy = "tree", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
public List<Fruit> fruits;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Fruit> getFruits() {
return fruits;
}
public void setFruits(List<Fruit> fruits) {
this.fruits = fruits;
}
public Long getTreeId() {
return treeId;
}
public void setTreeId(Long treeId) {
this.treeId = treeId;
}
}
@Entity
public class Fruit extends PanacheEntityBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long fruitId;
public String name;
public String color;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="treeId")
public Tree tree;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Tree getTree() {
return tree;
}
public void setTree(Tree tree) {
this.tree = tree;
}
public Long getFruitId() {
return fruitId;
}
public void setFruitId(Long fruitId) {
this.fruitId = fruitId;
}
}
Upvotes: 0
Views: 430
Reputation: 5196
Can you try setting the back-reference before persisting?
@RestController
public class TreeController {
@PostMapping("/tree")
@Transactional
public void addTree(Tree tree) {
for(Fruit fruit : tree.fruits)
fruit.tree = tree;
Tree.persist(tree);
}
@GetMapping("/tree")
public List<Tree> getTree() {
return Tree.listAll();
}
}
Upvotes: 2