在JPA / Hibernate中使用复合键连接表

时间:2011-05-17 19:59:51

标签: hibernate jpa join compound-key

好的,我们的数据库团队已经提出了以下结构。我无法改变它:

报告

  • 将参考编号(REF_NR)作为其PID
  • 加上有关报告,报告日期等的一些数据

属性

  • 将参考编号(REF_NR)建筑物编号(BLG_NR)作为其复合PID。报告的REF_NR将与报告中所有属性的REF_NR匹配,并由BLG_NR进一步编制索引。
  • 加上有关构建,平方英尺等的一些数据

施工数据

  • 有参考编号(REF_NR),建筑物编号(BLG_NR),结构类型编码(STRCT_TYP_CD)和材料类型编码(MTRL_TYP_CD)
  • 具有通常表示给定材料类型的给定结构类型的%ge的值。例如,50%的ROOF是SLATE,50%的ROOF是TILE,依此类推。

我正在努力加入这些报告,所有相关建筑物和每栋建筑的所有建筑数据,但我几乎是JPA& amp;休眠注释。

所以,请帮我确定合适的课程和注释,以便将这些内容结合在一起。或者只是一个很好的链接到一个非常好的例子会有所帮助。

@Entity
@Table(name="RCT_REPORT")
public class Report implements Serializable {
    private Set<Building> buildings;
    private long referenceNumber;

    @Id
    @Column(name="REF_NR")
    public long getReferenceNumber() {
        return this.referenceNumber;
    }
    public void setReferenceNumber(long referenceNumber) {
        this.referenceNumber = referenceNumber;
    }

    @OneToMany(mappedBy="report", fetch=FetchType.EAGER)
    public Set<Building> getBuildings() {
        return this.buildings;
    }
    public void setBuildings(Set<Building> buildings) {
        this.buildings = buildings;
    }   
}

@Entity
@Table(name="RCT_BUILDING")
public class Building implements Serializable {
    private Report report;
    private Set<ConstructionData> constructionData;
    private BuildingPK id;

    @EmbeddedId
    public BuildingPK getId() {
        return this.id;
    }

    public void setId(BuildingPK id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name="REF_NR", insertable=false, updatable=false)
    public Report getReport() {
        return this.report;
    }
    public void setReport(Report report) {
        this.report = report;
    }   

    @OneToMany(mappedBy="building", fetch=FetchType.EAGER)
    @JoinColumns ({
        @JoinColumn(name="REF_NR", referencedColumnName = "REF_NR"),
        @JoinColumn(name="BLG_NR", referencedColumnName = "BLG_NR")
    })
    public Set<ConstructionData> getConstructionData() {
        return this.constructionData;
    }

    public void setConstructionData(Set<ConstructionData> constructionData) {
        this.constructionData = constructionData;
    }   
}

@Embeddable
public class BuildingPK implements Serializable {
    private long referenceNumber;
    private int buildingNumber;

    @Column(name="REF_NR")
    public long getReferenceNumber() {
        return this.referenceNumber;
    }
    public void setReferenceNumber(long referenceNumber) {
        this.referenceNumber = referenceNumber;
    }

    @Column(name="BLG_NR")
    public int getBuildingNumber() {
        return this.buildingNumber;
    }
    public void setBuildingNumber(int buildingNumber) {
        this.buildingNumber = buildingNumber;
    }

    public boolean equals(Object other) {
        ...
    }

    public int hashCode() {
        ...
    }
}

@Entity
@Table(name="CNSTRCTN_DATA")
public class ConstructionData implements Serializable {
    private ConstructionDataPK id;
    private BigDecimal value;
    private Building building;

    @EmbeddedId
    public ConstructionDataPK getId() {
        return this.id;
    }
    public void setId(ConstructionDataPK id) {
        this.id = id;
    }


    @Column(name="MTRL_SEL_VAL")
    public BigDecimal getValue() {
        return this.value;
    }
    public void setValue(BigDecimal value) {
        this.value = value;
    }


    @ManyToOne
    @JoinColumns ({
        @JoinColumn(name="REF_NR", referencedColumnName="REF_NR", insertable=false, updatable=false),
        @JoinColumn(name="BLG_NR", referencedColumnName="BLG_NR", insertable=false, updatable=false)
    })
    public Building getBuilding() {
        return this.building;
    }

    public void setBuilding(Building building) {
        this.Building = building;
    }   
}

@Embeddable
public class ConstructionDataPK implements Serializable {
    private long referenceNumber;
    private int buildingNumber;
    private String structureTypeCode;
    private String materialTypeCode;

    @Column(name="REF_NR")
    public long getReferenceNumber() {
        return this.referenceNumber;
    }
    public void setReferenceNumber(long referenceNumber) {
        this.referenceNumber = referenceNumber;
    }

    @Column(name="BLG_NR")
    public int getbuildingNumber() {
        return this.buildingNumber;
    }
    public void setbuildingNumber(int buildingNumber) {
        this.buildingNumber = buildingNumber;
    }

    @Column(name="STRCT_TYP_CD")
    public String getStructureTypeCode() {
        return this.structureTypeCode;
    }
    public void setStructureTypeCode(String structureTypeCode) {
        this.structureTypeCode = structureTypeCode;
    }

    @Column(name="MTRL_TYP_CD")
    public String getMaterialTypeCode() {
        return this.materialTypeCode;
    }
    public void setMaterialTypeCode(String materialTypeCode) {
        this.materialTypeCode = materialTypeCode;
    }

    public boolean equals(Object other) {
        ...
    }

    public int hashCode() {
        ...
    }
}

正如我所说,我对此很陌生,这一切对我来说都是对的,但我确定我错过了什么。
谢谢! 彼得

1 个答案:

答案 0 :(得分:0)

看起来连接列属于关系中的“子”,而不是父级。我在两个地方都有它们。

另外,虽然它没有显示,但我有另一个问题,如果你只尝试映射复合键的一部分,hibernate 3.3.2不喜欢它。在我真正的问题中,在父母的PK中还有第三个字段,也在儿童和母语中。我没有在连接表中映射。一旦我添加它,一切都很好。

彼得