[英]Hibernate One to Many relation- Child table not updating
在這里,我向特定用戶添加新訂單列表。 在向特定用戶添加訂單時,它返回成功狀態,但我的數據庫仍然是空的。 為什么沒有添加數據...?
用戶
@Entity
Class Users{
@Id
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private List<Orders> orders;
}
訂單
@Entity
Class Orders{
@Id
private int id;
private Date orderDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
}
存儲庫
public ResponseEntity<String> addOrder(int userId,List<Orders> orders) throws UserNotFound{
User user =userRepository.findById(userId).orElse(null);
if(user==null) throw new UserNotFound("User Not Found");
for(Orders o:orders){
o.setOrderDate(new Date("....."));
}
user.getOrders().addAll(orders);
userRepository.save(user);
}
嘗試將級聯持久添加到您的關系規范中:
@Entity
Class Users{
@Id
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade={CascadeType.PERSIST})
private List<Orders> orders;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.