营销方案网站,朝阳区搜索优化seosem,宝塔服务器搭建网站教程,广告设计专业学校有哪些最近#xff0c;我们面临着带有复合id字段的休眠关联映射的棘手情况。 我们需要与一对一和多对一建立双向关联。我们的拖曳表是“ REPORT”和“ REPORT_SUMMARY”#xff0c;它们之间具有从REPORT到REPORT_SUMMARY的一对多关系#xff0c;而从REPORT_SUMMARY到REPORT表。 RE… 最近我们面临着带有复合id字段的休眠关联映射的棘手情况。 我们需要与一对一和多对一建立双向关联。我们的拖曳表是“ 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)
) ENGINEInnoDB DEFAULT CHARSETlatin1;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)
) ENGINEInnoDB DEFAULT CHARSETlatin1; 休眠实体类是休假的。 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 SetReportSummary reportSummaryList new HashSetReportSummary();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 SetReportSummary getReportSummaryList() {return reportSummaryList;}public void setReportSummaryList(SetReportSummary 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对象的引用。 此实现的最重要部分是休眠映射文件。 Report.hbm.xml ?xml version1.0?
!DOCTYPE hibernate-mapping PUBLIC
-//Hibernate/Hibernate Mapping DTD 3.0//EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
hibernate-mappingclass namecom.semika.autoac.entities.Report tableREPORT id nameid typeint columnid generator classnative//idproperty namenamecolumn nameNAME //propertyset namereportSummaryList tableREPORT_SUMMARY cascadeall inversetruekey columnRPT_ID not-nulltrue/keyone-to-many classcom.semika.autoac.entities.ReportSummary//set/class
/hibernate-mapping ReportSummary.hbm.xml ?xml version1.0?
!DOCTYPE hibernate-mapping PUBLIC
-//Hibernate/Hibernate Mapping DTD 3.0//EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtdhibernate-mappingclass namecom.semika.autoac.entities.ReportSummary tableREPORT_SUMMARY composite-id nameid classcom.semika.autoac.entities.ReportSummaryIdkey-property nameid columnID/key-propertykey-many-to-one namereport classcom.semika.autoac.entities.ReportcolumnRPT_ID/key-many-to-one/composite-idproperty namenamecolumn nameNAME //property/class
/hibernate-mapping 参考 如何在我们的JCG合作伙伴 Semika loku kaluge的Code Box博客上将Hibernate用于复合ID以及关联映射 。 翻译自: https://www.javacodegeeks.com/2012/08/hibernate-composite-ids-with.html