【发布时间】:2009-08-06 19:41:12
【问题描述】:
我正在尝试为“促销”映射一个对象模型;即,客户在注册期间输入促销代码。
不同的促销代码可能有不同类型的“好处”,即我们为客户提供的内容。 例如:促销代码 XYZ123 将在客户的帐户中为客户提供免费分钟,而不同的促销代码将为用户在注册时可以选择的各种订阅计划提供各种折扣。
在我的领域模型中,我将不同类型的好处建模为具有共同属性的抽象超类的子类。我选择将不同类型建模为子类,因为某些类型的好处需要不同的属性。
public abstract class Benefit {
//getters/setters for common attributes
}
public class FreeMinutesBenefit extends Benefit {
public int getFreeMinutes() {...}
public void setFreeMinutes(int minutes} {...}
}
public class PriceDiscountBenefit extends Benefit {
public Map<Plan, BigDecimal> getDiscountMap() {...}
public void setDiscountMap(Map<Plan, BigDecimal> map) {...}
}
粗略的 SQL 架构:
-- Parent table, maps promotion to benefits
create table Promo_Benefit (
map_id integer auto-generated PRIMARY KEY,
promo_id integer references PROMOTION(promo_id),
type_id integer references BENEFIT_TYPES(type_id)
);
create table BenefitDetails_FreeMinutes (
map_id integer PRIMARY KEY,
minutes integer not null,
FOREIGN KEY (map_id) references Promo_Benefit(map_id)
);
create table BenefitDetails_PriceDiscount (
map_id integer references Promo_Benefit(map_id),
plan_id integer references Plans(plan_id),
reduced_price numeric not null,
PRIMARY KEY (map_id, plan_id)
FOREIGN KEY (map_id) references Promo_Benefit(map_id)
);
我能够在我的 Hibernate 映射文件中成功映射每个子类的基本属性,但我无法弄清楚如何映射关联 在 PriceDiscountBenefit 子类和 Plan 类之间。
我想这是因为子类表 (BenefitDetails_PriceDiscount) 的主键不仅仅是 map_id 列 - 换句话说,这个子类表中的多行将形成一个单一的 PriceDiscountBenefit 实体。从我所见,Hibernate 中的每个子类表支持似乎是用于子类表中的单行映射到父表中的单行的实例 - 并且映射 <map> 应该指第二个包含键/值的表。
我映射这完全错误吗?我不知道我是否遇到了麻烦,因为我正在扼杀每个子类的表模式。
【问题讨论】:
-
从
Promo_Benefit表来看,你有一个Promotion 类,它与Benefit 是一对多的关系。这是真的吗?