我有这样的映射:
<!-- language: java -->
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
class CommonResource {
// mapping to all parents here
}
class CommonResourceChild extends CommonResource { }
class Parent1 {
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "parent1_common_resource_type1",
joinColumns = {@JoinColumn(name = "parent1_id")},
inverseJoinColumns = {@JoinColumn(name = "common_resource_id")})
List<CommonResource> commonResourcesType1;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "parent1_common_resource_type2",
joinColumns = {@JoinColumn(name = "parent1_id")},
inverseJoinColumns = {@JoinColumn(name = "common_resource_id")})
List<CommonResource> commonResourcesType2;
}
class Parent2 { //almost the same mapping, but with another parent
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "parent2_common_resource_type1",
joinColumns = {@JoinColumn(name = "parent2_id")},
inverseJoinColumns = {@JoinColumn(name = "common_resource_id")})
List<CommonResource> commonResourcesType1;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "parent2_common_resource_type2",
joinColumns = {@JoinColumn(name = "parent2_id")},
inverseJoinColumns = {@JoinColumn(name = "common_resource_id")})
List<CommonResource> commonResourcesType2;
}
@Inheritance(strategy = InheritanceType.JOINED)
abstract class AbstractParent3 { } // not every Parent3 have CommonResource
class Parent3Impl extends AbstractParent3 {
// some Parent2 can have CommonResource or it's children
@OneToOne(cascade = CascadeType.ALL)
CommonResourceChild commonResourceChild;
}
是否可以根据以下几点以某种方式将每个 CommonResource 实体及其子类型映射到不同的父级?
- 一些父母有 CommonResources 或其孩子的列表,
- 一些父母(或他们的孩子)有一个 CommonResources 或其孩子的实例,
我可以随意更改映射或修改数据库,因为我还没有发布它。:)