在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.
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.