Hibernate映射到同一个表的几个关系

时间:2013-12-28 00:23:25

标签: java spring hibernate oracle11g hibernate-mapping

我有以下ER: Entity Relationship

我想知道如何使用hibernate映射第三个表。 我有这段代码:

@ManyToOne(optional = false)
@JoinColumns({
  @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false),
  @JoinColumn(name = "tbl3_tbl2_id", referencedColumnName = "tbl2_id", insertable = false, updatable = false)})
public TableDB getTableDB() {
   return tableDB;
}

@OneToOne(optional = false)
@JoinColumns({
  @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false),
  @JoinColumn(name = "tbl3_tbl2_id_2", referencedColumnName = "tbl2_id", insertable = false, updatable = false)})
public TableDB getTableDB2() {
   return tableDB2;
}

@OneToOne(optional = false)
@JoinColumns({
  @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false),
  @JoinColumn(name = "tbl3_tbl2_id_3", referencedColumnName = "tbl2_id", insertable = false, updatable = false)})
public TableDB getTableDB3() {
   return tableDB3;
}

问题在于一对一2和3。 当我调用方法“保存”时,插入代码是这样的:

insert into table3 (tbl3_desc, tbl3_id, tbl3_tbl1_id, tbl3_tbl2_id) values (?, ?, ?, ?)

tbl3_tbl2_id_2, tbl3_tbl2_id_3在哪里?

有任何意见或建议吗?

1 个答案:

答案 0 :(得分:0)

如果我们采取具体的例子如下:

table1的地图 - > COMPANIES

  import java.io.Serializable;
  import java.util.List;
  import javax.persistence.Basic;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;

  @Entity
  @Table(name = "COMPANIES")
  public class Company implements Serializable {
     private static final long serialVersionUID = 1L;
     @Id
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 4)
     @Column(name = "COMP_KEY")
     private String key;
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 100)
     @Column(name = "COMP_DESCRIPTION")
     private String description;
     @OneToMany(cascade = CascadeType.ALL, mappedBy = "company", fetch = FetchType.LAZY)
     private List<Branch> branches;
     //GETTERS, SETTERS...
  }

table2的地图 - &gt; BRANCHES

  import java.io.Serializable;
  import java.util.List;
  import javax.persistence.Basic;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.EmbeddedId;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;

  @Entity
  @Table(name = "BRANCHES")
  public class Branch implements Serializable {
     private static final long serialVersionUID = 1L;
     @EmbeddedId
     protected BranchPK branchPK;
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 100)
     @Column(name = "BRAN_DESCRIPTION")
     private String description;
     @JoinColumn(name = "BRAN_COMP_KEY", referencedColumnName = "COMP_KEY")
     @ManyToOne(optional = false, fetch = FetchType.LAZY)
     private Company company;
     @OneToMany(cascade = CascadeType.ALL, mappedBy = "branch", fetch = FetchType.LAZY)
     private List<Employee> staff;
     //GETTERS, SETTERS, ETC
  }

  import java.io.Serializable;
  import javax.persistence.Basic;
  import javax.persistence.Column;
  import javax.persistence.Embeddable;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;

  @Embeddable
  public class BranchPK implements Serializable {
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 4)
     @Column(name = "BRAN_COMP_KEY")
     private String companyKey;
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 4)
     @Column(name = "BRAN_KEY")
     private String brachKey;
     //GETTERS, SETTERS, ETC
  }

map3的地图 - &gt; STAFF

  import java.io.Serializable;
  import javax.persistence.Basic;
  import javax.persistence.Column;
  import javax.persistence.EmbeddedId;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinColumns;
  import javax.persistence.ManyToOne;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;

  @Entity
  @Table(name = "STAFF")
  public class Employee implements Serializable {
     private static final long serialVersionUID = 1L;
     @EmbeddedId
     protected EmployeePK employeePK;
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 15)
     @Column(name = "EMPL_NAME")
     private String name;

     @JoinColumns({
        @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false),
        @JoinColumn(name = "EMPL_BRAN_KEY", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)})
     @ManyToOne(optional = false, fetch = FetchType.EAGER)
     private Branch branch;
     @JoinColumns({
        @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false),
        @JoinColumn(name = "EMPL_BRAN_SUPER", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)})
     @OneToOne(optional = false)
     private Branch branchSupervice;
     @JoinColumns({
        @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false),
        @JoinColumn(name = "EMPL_BRAN_VISIT", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)})
     @OneToOne
     private Branch branchVisitor;
     //GETTERS, SETTERS, ETC
  }

  import java.io.Serializable;
  import javax.persistence.Basic;
  import javax.persistence.Column;
  import javax.persistence.Embeddable;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;

  @Embeddable
  public class EmployeePK implements Serializable {
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 4)
     @Column(name = "EMPL_COMP_KEY")
     private String companyKey;
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 4)
     @Column(name = "EMPL_BRAN_KEY")
     private String branchKey;
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 4)
     @Column(name = "EMPL_KEY")
     private String employeeKey;
     //GETTERS, SETTERS, ETC
  }

我为这个案子提供了“家庭补救”解决方案。 我为每个关联对象的主键创建了2个额外字段。

  @Entity
  @Table(name = "STAFF")
  public class Employee implements Serializable {
     private static final long serialVersionUID = 1L;
     //Besides the above mentioned code
     @Basic(optional = false)
     @NotNull
     @Size(min = 1, max = 4)
     @Column(name = "EMPL_BRAN_SUPER")
     private String keyBranchSupervice;
     @Size(max = 4)
     @Column(name = "EMPL_BRAN_VISIT")
     private String keyBranchVisitor;

     public void setBranchSupervice(Branch branchSupervice) {
        this.keyBranchSupervice = branchSupervice.branchPK.getBrachKey();
        this.branchSupervice = branchSupervice;
     }
  }

并且对于对象的setBranchSupervice上的相同FK中的每个字段指定,我等于值。 有了这个,我可以顺利添加,编辑,删除。

但我仍然怀疑应该是(良好做法)。