Spring data jpa怎麼配置一個實體類對映兩張資料庫表
阿新 • • 發佈:2019-01-03
今天寫一個Spring boot整合Spring data jpa實現一些簡單功能的技術驗證專案,其中一個Model類Wel映射了“T_PM_WELL”表,但有一個屬性"sname"需要對映到第二張表“T_PM_NODE”上,開始時用@Formula註解來實現,但執行時總是會報錯,百度了很久也沒解決,後來下了jpa的文件,發現了"@SecondaryTable"註解正適合我的場景下使用,而@Formula通常用來對映到一個虛擬列上,而非實際存在的列,例如需要查詢一個count(*),然後儲存到某個屬性上時,就應該用@Formula註解,但因為我還不知道怎麼正確使用這個註解,今天就略過這個註解不講了,直接上程式碼,看下@SecondaryTable註解的使用吧
還有一個@SecondaryTables註解用來對映多張表的,因為我今天用不上,所以沒有具體使用的程式碼可貼,具體使用可以看下jpa的文件@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; } }
程式設計技術交流請加QQ群:點選連結加入群【Just Do IT】:https://jq.qq.com/?_wv=1027&k=478lBF3