简体   繁体   English

休眠一对多和多对一关系

[英]Hibernate One to Many and Many to One Relation

These two questions answered many of my questions, but I am still struggling to think about in real scenario! 这两个问题回答了我的许多问题,但我仍在努力思考真实的情况!

Taking an example from the references. 以参考文献为例。 Assume I have one Order and Multiple Items associated with it. 假设我有一个订单和与其关联的多个项目。 Now assume One Item can have one Returns but one Returns can have multiple Items. 现在假设一件物品可以有一个退货,但是一件物品可以有多个退货。

What I understood is, Order to Items will be One to Many Relation . 我了解的是, Order to Items will be One to Many Relation Since I need to get Order of an Item, I will create column 'order_fk' in Item table to get it. 由于我需要获取商品的订单,因此我将在商品表中创建列“ order_fk”来获取商品。

//Order entity
@OneToMany
@JoinColumn(name = "order_fk")
private List<Items> items;

//item entity
@Column(name = "order_fk")
private Long orderId;

Return to Items is One to Many mapping. One Return can have multiple Items. But one Item can have only one return id

//Return entity
@OneToMany
@JoinColumn(name = "return_fk")
private List<Items> items;

//item entity
@Column(name = "return_fk")
private Long returnId;

Am I thinking in the right direction? 我在想正确的方向吗? Please make me understand this relations and uni/bi-directional relationships. 请让我理解这种关系和单向/双向关系。

Overall, I should get Items for an Order. 总的来说,我应该为订单获取物品。 Get Orderid of given Item. 获取给定物品的Orderid。 Get Items of Returns and get returnId of given Item. 获取退货商品并获取给定商品的returnId。

Reference: 参考:

  1. Difference Between One-to-Many, Many-to-One and Many-to-Many? 一对多,多对一和多对多的区别? Hibernate/JPA ManyToOne vs OneToMany Hibernate / JPA ManyToOne与OneToMany

This should be the correct mapping of the entities (database tables and columns are ok) 这应该是实体的正确映射(数据库表和列都可以)

//Order entity
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private List<Items> items;

//item entity
@ManyToOne
@Column(name = "order_fk")
private Order order;

//Return entity
@OneToMany(mappedBy = "return")
private List<Items> items;

//item entity
@ManyToOne
@Column(name = "return_fk")
private Return return;

cascade = CascadeType.ALL in first mapping means whenever you save/update/delete an order, its items will also be saved/updated/deleted, so adjust it to your needs, on other mapping as well. 第一个映射中的cascade = CascadeType.ALL意味着每当您保存/更新/删除订单时,其项目也将被保存/更新/删除,因此也可以在其他映射上根据您的需要进行调整。

Unidirectional relations mean only one side of the relation is aware of the other side. 单向关系意味着关系的只有一侧知道另一侧。 On your examples, if you removed items from Return entity you would have an unidirectional relation between Item and Return . 在示例中,如果从Return实体中删除了items ,则ItemReturn之间将具有单向关系。 With items present, you have a bidirectional relation. 在存在items ,您具有双向关系。

I think you should use OneToMany in another way: 我认为您应该以另一种方式使用OneToMany:

// Order entity
@OneToMany(mappedBy = "columnInItemsPointingAtOrders")
private List<Items> items;

Please check the docs: http://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html . 请检查文档: http : //docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html

And one more thing - you are not geting the IDs but entities: 还有一件事-您没有获得ID,而是实体:

//item entity
@Column(name = "order_fk")
private Order order;

I'm also new to this subject. 我也是这个主题的新手。 What helped me to understand the relations is drawing the EER diagram and then synchronizing it to the test DB and experimenting. 帮助我理解这些关系的是绘制EER图,然后将其同步到测试数据库并进行实验。 This does not answer your question but may give a direction. 这不能回答您的问题,但可以提供指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM