Hibernate映射ManyToOne与许多父母

时间:2013-10-03 13:51:55

标签: java hibernate inheritance one-to-many many-to-one

我有这样的映射:

<!-- 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实体及其子类型映射到不同的父母?

  1. 有些家长有CommonResources或其子女的名单,
  2. 有些父母(或他们的孩子)有一个CommonResources实例或其子代,
  3. 我可以自由更改映射或修改数据库,因为我还没有发布它。 :)

0 个答案:

没有答案