尝试来自" 7.2.2.2。地图"来自Hibernate 4.3文档,我有两个实体:
@Entity
public class Order {
@Id
@GeneratedValue
private Integer id;
String number;
@ManyToOne
private Customer customer;
//getters/setters
}
@Entity
public class Customer {
@Id
@GeneratedValue
private Integer id;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
@MapKey(name = "number")
private Map<String, Order> orders;
//getters/setters
}
添加cascade = CascadeType.ALL
后,它会启动保存订单实体,但属性number
和customer
仍然保存为null
我如何尝试保存:
session.beginTransaction();
Map<String, Order> map = new HashMap<>();
map.put("0", new Order());
map.put("1", new Order());
map.put("2", new Order());
map.put("3", new Order());
Customer customer = new Customer();
customer.setOrders(map);
session.save(customer);
session.getTransaction().commit();
出了什么问题?
答案 0 :(得分:0)
您没有在任何地方初始化订单的number
属性,因为类型String
的默认值为null
,因此您在db中获得空值。