Spring data jpa怎么配置一个实体类映射两张数据库表

今天写一个Spring boot集成Spring data jpa实现一些简单功能的技术验证项目,其中一个Model类Wel映射了“T_PM_WELL”表,但有一个属性"sname"需要映射到第二张表“T_PM_NODE”上,开始时用@Formula注解来实现,但运行时总是会报错,百度了很久也没解决,后来下了jpa的文档,发现了"@SecondaryTable"注解正适合我的场景下使用,而@Formula通常用来映射到一个虚拟列上,而非实际存在的列,例如需要查询一个count(*),然后保存到某个属性上时,就应该用@Formula注解,但因为我还不知道怎么正确使用这个注解,今天就略过这个注解不讲了,直接上代码,看下@SecondaryTable注解的使用吧

@Entity
@Table(name="T_PM_WELL")
@SecondaryTable(name="T_PM_NODE")
public class Well {

    @Id
    @Column(name="WELL_ID")
    private Integer wellId;

    @Column(name="WELLTYPE_ID")
    private Integer wellTypeId;

    @Column(name="WELLDIST_ID")
    private Integer wellDistId;

    @Column(name="WELLMODEL_ID")
    private Integer wellModelId;

    @Column(name="BLOCK_ID")
    private Integer blockId;

    @Column(name="GEOLOGY_ID")
    private Integer geologyId;

    @Column(name="WELLFINISH_ID")
    private Integer wellFinishId;

    @Column(name="PLATFORM_ID")
    private Integer platformId;

    @Column(name="PROGEOLOGY_ID")
    private Integer progeologyId;

    @Column(name="MINING_METHOD")
    private Integer miningMethod;

    @Column(name="WELL_DEPTH")
    private String wellDepth;

    @Column(name="WORK_SEGMENT")
    private String workSegment;

    @Column(name="PRODUCT_START_DATE")
    private Date productStartDate;

    @Column(name="ON_OFF")
    private Integer onOff;

    @Column(name="METE_TYPE")
    private Integer meteType;

    @Column(name="SEGREGATOR_EQUIP_CODE")
    private String segregatorEquipCode;

    @Column(name="INST_INSTR_ID")
    private String instInstrId;

    @Column(name="ACCU_INSTR_ID")
    private String accuInstrId;

    @Column(name="FURNACE_EQUIP_CODE")
    private String furnaceEquipCode;

    @Column(name="USEGAS_INSTR_ID")
    private String usegasInstrId;

    @Column(name="ACCU_WATERINSTR_ID")
    private String accuWaterinstrId;

    @Column(name="INST_WATERINSTR_ID")
    private String instWaterinstrId;

    @Column(name="SNAME", table="T_PM_NODE")
    private String sName;


    public String getsName() {
        return sName;
    }


    public void setsName(String sName) {
        this.sName = sName;
    }


    public Integer getWellId() {
        return wellId;
    }


    public Integer getWellTypeId() {
        return wellTypeId;
    }


    public Integer getWellDistId() {
        return wellDistId;
    }


    public Integer getWellModelId() {
        return wellModelId;
    }


    public Integer getBlockId() {
        return blockId;
    }


    public Integer getGeologyId() {
        return geologyId;
    }


    public Integer getWellFinishId() {
        return wellFinishId;
    }


    public Integer getPlatformId() {
        return platformId;
    }


    public Integer getProgeologyId() {
        return progeologyId;
    }


    public Integer getMiningMethod() {
        return miningMethod;
    }


    public String getWellDepth() {
        return wellDepth;
    }


    public String getWorkSegment() {
        return workSegment;
    }


    public Date getProductStartDate() {
        return productStartDate;
    }


    public Integer getOnOff() {
        return onOff;
    }


    public Integer getMeteType() {
        return meteType;
    }


    public String getSegregatorEquipCode() {
        return segregatorEquipCode;
    }


    public String getInstInstrId() {
        return instInstrId;
    }


    public String getAccuInstrId() {
        return accuInstrId;
    }


    public String getFurnaceEquipCode() {
        return furnaceEquipCode;
    }


    public String getUsegasInstrId() {
        return usegasInstrId;
    }


    public String getAccuWaterinstrId() {
        return accuWaterinstrId;
    }


    public String getInstWaterinstrId() {
        return instWaterinstrId;
    }


    public void setWellId(Integer wellId) {
        this.wellId = wellId;
    }


    public void setWellTypeId(Integer wellTypeId) {
        this.wellTypeId = wellTypeId;
    }


    public void setWellDistId(Integer wellDistId) {
        this.wellDistId = wellDistId;
    }


    public void setWellModelId(Integer wellModelId) {
        this.wellModelId = wellModelId;
    }


    public void setBlockId(Integer blockId) {
        this.blockId = blockId;
    }


    public void setGeologyId(Integer geologyId) {
        this.geologyId = geologyId;
    }


    public void setWellFinishId(Integer wellFinishId) {
        this.wellFinishId = wellFinishId;
    }


    public void setPlatformId(Integer platformId) {
        this.platformId = platformId;
    }


    public void setProgeologyId(Integer progeologyId) {
        this.progeologyId = progeologyId;
    }


    public void setMiningMethod(Integer miningMethod) {
        this.miningMethod = miningMethod;
    }


    public void setWellDepth(String wellDepth) {
        this.wellDepth = wellDepth;
    }


    public void setWorkSegment(String workSegment) {
        this.workSegment = workSegment;
    }


    public void setProductStartDate(Date productStartDate) {
        this.productStartDate = productStartDate;
    }


    public void setOnOff(Integer onOff) {
        this.onOff = onOff;
    }


    public void setMeteType(Integer meteType) {
        this.meteType = meteType;
    }


    public void setSegregatorEquipCode(String segregatorEquipCode) {
        this.segregatorEquipCode = segregatorEquipCode;
    }


    public void setInstInstrId(String instInstrId) {
        this.instInstrId = instInstrId;
    }


    public void setAccuInstrId(String accuInstrId) {
        this.accuInstrId = accuInstrId;
    }


    public void setFurnaceEquipCode(String furnaceEquipCode) {
        this.furnaceEquipCode = furnaceEquipCode;
    }


    public void setUsegasInstrId(String usegasInstrId) {
        this.usegasInstrId = usegasInstrId;
    }


    public void setAccuWaterinstrId(String accuWaterinstrId) {
        this.accuWaterinstrId = accuWaterinstrId;
    }


    public void setInstWaterinstrId(String instWaterinstrId) {
        this.instWaterinstrId = instWaterinstrId;
    }
}
还有一个@SecondaryTables注解用来映射多张表的,因为我今天用不上,所以没有具体使用的代码可贴,具体使用可以看下jpa的文档



编程技术交流请加QQ群:点击链接加入群【Just Do IT】:https://jq.qq.com/?_wv=1027&k=478lBF3

你可能感兴趣的:(JAVA,Spring,data,jpa,配置,一个实体类映射两张数据库表,SecondaryTable)