具有关联映射的Hibernate Composite ID
生活随笔
收集整理的這篇文章主要介紹了
具有关联映射的Hibernate Composite ID
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近,我們面臨著帶有復(fù)合id字段的休眠關(guān)聯(lián)映射的棘手情況。 我們需要與一對一和多對一建立雙向關(guān)聯(lián)。我們的拖曳表是“ REPORT”和“ REPORT_SUMMARY”,它們之間具有從REPORT到REPORT_SUMMARY的一對多關(guān)系,而從REPORT_SUMMARY到REPORT表。 REPORT_SUMMARY表的主鍵定義為復(fù)合主鍵,它由自動增量id字段和REPORT表的主鍵組成。 CREATE TABLE REPORT (ID INT(10) NOT NULL AUTO_INCREMENT,NAME VARCHAR(45) NOT NULL,PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;CREATE TABLE REPORT_SUMMARY (ID INT(10) NOT NULL AUTO_INCREMENT,NAME VARCHAR(45) NOT NULL,RPT_ID INT(10) NOT NULL,PRIMARY KEY (`ID`,`RPT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
休眠實體類是休假的。
Report.java
ReportSummary.java
package com.semika.autoac.entities;import java.io.Serializable; public class ReportSummary implements Serializable {private static final long serialVersionUID = 8052962961003467437L;private ReportSummaryId id; private String name;public ReportSummaryId getId() {return id; } public void setId(ReportSummaryId id) {this.id = id; } public String getName() {return name; } public void setName(String name) {this.name = name; } @Override public int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());return result; } @Override public boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;ReportSummary other = (ReportSummary) obj;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;} }ReportSummaryId.java
package com.semika.autoac.entities;import java.io.Serializable;public class ReportSummaryId implements Serializable{private static final long serialVersionUID = 6911616314813390449L;private Integer id; private Report report;public Integer getId() {return id; } public void setId(Integer id) {this.id = id; } public Report getReport() {return report; } public void setReport(Report report) {this.report = report; } @Override public int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((report == null) ? 0 : report.hashCode());return result; } @Override public boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;ReportSummaryId other = (ReportSummaryId) obj;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (report == null) {if (other.report != null)return false;} else if (!report.equals(other.report))return false;return true;} }報表對象具有ReportSummary對象的集合,ReportSummaryId具有對Report對象的引用。 此實現(xiàn)的最重要部分是休眠映射文件。 Report.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping><class name="com.semika.autoac.entities.Report" table="REPORT" ><id name="id" type="int" column="id" ><generator class="native"/></id><property name="name"><column name="NAME" /></property><set name="reportSummaryList" table="REPORT_SUMMARY" cascade="all" inverse="true"><key column="RPT_ID" not-null="true"></key><one-to-many class="com.semika.autoac.entities.ReportSummary"/></set></class> </hibernate-mapping>ReportSummary.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.semika.autoac.entities.ReportSummary" table="REPORT_SUMMARY" ><composite-id name="id" class="com.semika.autoac.entities.ReportSummaryId"><key-property name="id" column="ID"></key-property><key-many-to-one name="report" class="com.semika.autoac.entities.Report"column="RPT_ID"</key-many-to-one></composite-id><property name="name"><column name="NAME" /></property></class> </hibernate-mapping>參考: 如何在我們的JCG合作伙伴 Semika loku kaluge的Code Box博客上將Hibernate用于復(fù)合ID以及關(guān)聯(lián)映射 。
翻譯自: https://www.javacodegeeks.com/2012/08/hibernate-composite-ids-with.html
總結(jié)
以上是生活随笔為你收集整理的具有关联映射的Hibernate Composite ID的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos如何获取隐秘信息(ddos如何获
- 下一篇: 培训学校备案凭证是什么意思(培训学校备案