没有joincolumn / jointable的Hibbernate映射映射

时间:2015-10-23 20:52:53

标签: java hibernate dictionary mapping persistence

尝试来自" 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后,它会启动保存订单实体,但属性numbercustomer仍然保存为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();

出了什么问题?

1 个答案:

答案 0 :(得分:0)

您没有在任何地方初始化订单的number属性,因为类型String的默认值为null,因此您在db中获得空值。