我有一个分层映射问题。概念上3个元素
[约束:我必须将两种类型的产品保持为相同的实体类。最终将作为Web服务发布。因此,首选定义/模式。]
产品规格 - 产品详细说明
产品成分 - 如果产品由一种或多种中间产品构成,则保持这种关系。作文也有其他一些属性。
class ProductDefinition {
@ID
private Long id;
@Column(name="product_code")
private String productCode;
@Column(name="product_type")
private String productType; // STANDARD, COMPOSITE
@OneToMany(mappedBy="productDefinitionRef", cascade={CascadeType.ALL})
private Set<ProductSpec> productSpecs;
@OneToMany(mappedBy="childProductRef", cascade={CascadeType.ALL})
private Set<ProductComposition> compositionRefs; // For a standard product, lists the composites where the product belongs to
@OneToMany(mappedBy="parentProductRef", cascade={CascadeType.ALL})
private Set<ProductComposition> childProducts; // For a composite product, lists the proucts which are part of the composite
// ... other attributes
}
class ProductComposition {
@ID
private Long id;
private ProductDefinition
@ManyToOne
@JoinColumn(name="parent_prod_ref")
private ProductDefinition parentProductRef;
@ManyToOne
@JoinColumn(name="child_prod_ref")
private ProductDefinition childProductRef;
// ... Other attributes
}
class ProductSpec {
@ID
private Long id;
@ManyToOne
@JoinColumn(name="prod_def_ref")
private ProductDefinition productDefinitionRef;
// .. other attributes
}
我期望从上述模型获得的一些样本数据:
产品定义: -
| ID | Code | Type | productSpecs | compositionRefs | childProducts |
| 1001 | P1 | STANDARD | [20001] | Empty | Empty |
| 1002 | P2 | STANDARD | [20002] | [2001] | Empty |
| 1003 | P3 | STANDARD | [20003] | [2001, 2002]| Empty |
| 1004 | P4 | STANDARD | [20004] | Empty | Empty |
| 2001 | B1 | COMPOSITE | Empty | Empty | [1002] |
| 2002 | B2 | COMPOSITE | Empty | Empty | [1002, 1003] |
产品成分: -
| ID | parentProductRef | childProductRef |
| 30005 | 2001 | 1002 |
| 30006 | 2002 | 1002 |
| 30007 | 2002 | 1003 |
问题: -