日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate关联映射_具有关联映射的Hibernate Composite ID

發布時間:2023/12/3 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate关联映射_具有关联映射的Hibernate Composite ID 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

hibernate關聯映射

最近,我們面臨著帶有復合id字段的Hibernate關聯映射的棘手情況。 我們需要與一對一和多對一進行雙向關聯。我們的拖曳表是“ REPORT”和“ REPORT_SUMMARY”,它們之間具有從REPORT到REPORT_SUMMARY的一對多關系,而從REPORT_SUMMARY到REPORT表。 REPORT_SUMMARY表的主鍵定義為復合主鍵,它由自動增量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;

Hibernate實體類是休假的。
Report.java

package com.semika.autoac.entities;import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Report implements Serializable{private static final long serialVersionUID = 9146156921169669644L;private Integer id;private String name;private Set<ReportSummary> reportSummaryList = new HashSet<ReportSummary>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<ReportSummary> getReportSummaryList() {return reportSummaryList;}public void setReportSummaryList(Set<ReportSummary> reportSummaryList) {this.reportSummaryList = reportSummaryList;} }

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對象的引用。 此實現的最重要部分是Hibernate映射文件。 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用于復合ID以及關聯映射 。


翻譯自: https://www.javacodegeeks.com/2012/08/hibernate-composite-ids-with.html

hibernate關聯映射

總結

以上是生活随笔為你收集整理的hibernate关联映射_具有关联映射的Hibernate Composite ID的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。