在ORM中,many-to-one和one-to-many

在ORM中,one-to-one和many-to-one都是最简单的(关键就是最后的那个[color=red][b]xx-to-one[/b][/color]),他们都表示以下的类对应关系:
public class PendingOrder {
private Address deliveryAddress
private Restaurant restaurant;
}

PendingOrder 对应的表中只要增加一列来存放Address 或者Restaurant 对应的外键即可.

另外,记录一下pojo in action中对one-to-many的解释(on page 134):
A relationship called A-B is one-to-many when each B
object is only referenced by a single A object.

public class PendingOrder {
private List lineItems; /* List<PendingOrderLineItem> */

}
A one-to-many relationship is usually mapped using a foreign key in the referenced
class’s table.

We can, for instance, map the PendingOrder lineItems field
using a foreign key in the PENDING_ORDER_LINE_ITEM table:
CREATE TABLE PENDING_ORDER_LINE_ITEM (

PENDING_ORDER_ID NUMBER(10)
LINE_ITEM_INDEX NUMBER(10) NOT NULL,
CONSTRAINT P_ORD_LINE_ITEM_ORDER_FK
FOREIGN KEY(PENDING_ORDER_ID)
REFERENCES PENDING_ORDER(PENDING_ORDER_ID)

The PENDING_ORDER_LINE_ITEM table has a PENDING_ORDER_ID column, which is
a foreign key to the PENDING_ORDER table.

下面这段话很精辟,同样来自pojo in action(on page 135):
One-to-many relationships are often whole-part relationships, which are relationships
where the part cannot exist independently of the whole. A part must be
deleted if either the whole is deleted or the part is no longer associated with the
whole. Examples of whole-part relationships in the Food to Go domain model are
PendingOrder-PendingOrderLineItem and Restaurant-MenuItem. A line item or
menu item cannot exist independently of its PendingOrder or Restaurant. As I
describe later, it is extremely useful if an ORM framework directly supports wholepart
relationships.

感觉pojo in action 对各种ORM的关系说明的还是很清晰的,但是具体怎么映射还是要看hibernate in action.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值