Hibernate多对多,同时包含一对多,双向关联保存实例。
一。介绍
此例子是本人在项目中涉及到的例子。主要拿出来3张表来解释Hibernate一对多以及多对多的保存操作。
表介绍:
用户表(ITYW_USER);工作表(CHECK_WORK);工作表和用户表的关联表(CHECKWORK_OPERATOR)
二.实体
package com.inpoup.itoma.entity.user; import java.util.HashSet; import java.util.Set; import com.inpoup.itoma.entity.user.Checkin; import com.inpoup.itoma.entity.task.TodayTask; import com.inpoup.itoma.entity.work.AssignWork; import com.inpoup.itoma.entity.work.CheckWork; import com.inpoup.itoma.entity.work.CheckWorkRecord; import com.inpoup.itoma.entity.work.CheckWorkRecordTemplate; import com.inpoup.itoma.entity.work.DailyWork; import com.inpoup.itoma.entity.work.DailyWorkRecord; import com.inpoup.itoma.entity.work.DailyWorkRecordTemplate; import com.inpoup.itoma.entity.work.PersonalWork; /** * 人员表对应实体类 * @author X宝龙 * @version 1.0 * @since 2013-06-07 * */ public class User { private int id; //ID private String account; //帐户 private String password; //密码 private String name; //姓名 private String sex; //性别 private String tel; //电话 private String email; //E-Mail private String depart; //部门 private String company; //公司 private int state; //状态 private Set<CheckWork> makeCheckWorks = new HashSet<CheckWork>(); private Set<CheckWork> actionCheckWorks = new HashSet<CheckWork>(); private Set<AssignWork> makeAssignWorks = new HashSet<AssignWork>(); private Set<DailyWork> makeDailyWorks = new HashSet<DailyWork>(); private Set<DailyWork> actionDailyWorks = new HashSet<DailyWork>(); private Set<PersonalWork> personalWorks = new HashSet<PersonalWork>(); private Set<Role> roles = new HashSet<Role>(); private Set<CheckWorkRecordTemplate> checkWorkRecordTemplates = new HashSet<CheckWorkRecordTemplate>(); private Set<CheckWorkRecord> checkWorkRecords = new HashSet<CheckWorkRecord>(); private Set<DailyWorkRecord> dailyWorkRecords = new HashSet<DailyWorkRecord>(); private Set<DailyWorkRecordTemplate> dailyWorkRecordTemplates = new HashSet<DailyWorkRecordTemplate>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getDepart() { return depart; } public void setDepart(String depart) { this.depart = depart; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public int getState() { return state; } public void setState(int state) { this.state = state; } public Set<CheckWork> getMakeCheckWorks() { return makeCheckWorks; } public void setMakeCheckWorks(Set<CheckWork> makeCheckWorks) { this.makeCheckWorks = makeCheckWorks; } public Set<CheckWork> getActionCheckWorks() { return actionCheckWorks; } public void setActionCheckWorks(Set<CheckWork> actionCheckWorks) { this.actionCheckWorks = actionCheckWorks; } public Set<AssignWork> getMakeAssignWorks() { return makeAssignWorks; } public void setMakeAssignWorks(Set<AssignWork> makeAssignWorks) { this.makeAssignWorks = makeAssignWorks; } public Set<DailyWork> getMakeDailyWorks() { return makeDailyWorks; } public void setMakeDailyWorks(Set<DailyWork> makeDailyWorks) { this.makeDailyWorks = makeDailyWorks; } public Set<DailyWork> getActionDailyWorks() { return actionDailyWorks; } public void setActionDailyWorks(Set<DailyWork> actionDailyWorks) { this.actionDailyWorks = actionDailyWorks; } public Set<PersonalWork> getPersonalWorks() { return personalWorks; } public void setPersonalWorks(Set<PersonalWork> personalWorks) { this.personalWorks = personalWorks; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } public Set<CheckWorkRecordTemplate> getCheckWorkRecordTemplates() { return checkWorkRecordTemplates; } public void setCheckWorkRecordTemplates( Set<CheckWorkRecordTemplate> checkWorkRecordTemplate) { this.checkWorkRecordTemplates = checkWorkRecordTemplate; } public Set<CheckWorkRecord> getCheckWorkRecords() { return checkWorkRecords; } public void setCheckWorkRecords(Set<CheckWorkRecord> checkWorkRecord) { this.checkWorkRecords = checkWorkRecord; } public Set<DailyWorkRecord> getDailyWorkRecords() { return dailyWorkRecords; } public void setDailyWorkRecords(Set<DailyWorkRecord> dailyWorkRecords) { this.dailyWorkRecords = dailyWorkRecords; } public Set<DailyWorkRecordTemplate> getDailyWorkRecordTemplates() { return dailyWorkRecordTemplates; } public void setDailyWorkRecordTemplates( Set<DailyWorkRecordTemplate> dailyWorkRecordTemplates) { this.dailyWorkRecordTemplates = dailyWorkRecordTemplates; } }
package com.inpoup.itoma.entity.work; import java.util.Date; import java.util.HashSet; import java.util.Set; import com.inpoup.itoma.entity.user.User; /** * 巡检工作表对应实体类 * @author 常宝龙 * @version 1.0 * @since 2013-06-07 * */ public class CheckWork{ private int id; //ID private Date planStartTime; //计划开始时间 private Date planEndTime; //计划结束时间 private String operationStartTime; //执行开始时间 private String operationEndTime; //执行结束时间 private String workCycle; //工作周期 private String workDay; //工作日 private String cusRemind; //定制提醒 private int remindTimes; //提醒次数 private int remindCycle; //提醒周期 private String workCont; //工作内容 private User user; //计划制定人(many-to-one关系) private Date planMakeTime; //计划制定时间 private int planState; //计划状态 private int state; //状态 private Set<User> users = new HashSet<User>(); private Set<CheckWorkRecordTemplate> checkWorkRecordTemplate = new HashSet<CheckWorkRecordTemplate>(); private Set<CheckWorkRecord> checkWorkRecord = new HashSet<CheckWorkRecord>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getPlanStartTime() { return planStartTime; } public void setPlanStartTime(Date planStartTime) { this.planStartTime = planStartTime; } public Date getPlanEndTime() { return planEndTime; } public void setPlanEndTime(Date planEndTime) { this.planEndTime = planEndTime; } public String getOperationStartTime() { return operationStartTime; } public void setOperationStartTime(String operationStartTime) { this.operationStartTime = operationStartTime; } public String getOperationEndTime() { return operationEndTime; } public void setOperationEndTime(String operationEndTime) { this.operationEndTime = operationEndTime; } public String getWorkCycle() { return workCycle; } public void setWorkCycle(String workCycle) { this.workCycle = workCycle; } public String getWorkDay() { return workDay; } public void setWorkDay(String workDay) { this.workDay = workDay; } public String getCusRemind() { return cusRemind; } public void setCusRemind(String cusRemind) { this.cusRemind = cusRemind; } public int getRemindTimes() { return remindTimes; } public void setRemindTimes(int remindTimes) { this.remindTimes = remindTimes; } public int getRemindCycle() { return remindCycle; } public void setRemindCycle(int remindCycle) { this.remindCycle = remindCycle; } public String getWorkCont() { return workCont; } public void setWorkCont(String workCont) { this.workCont = workCont; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Date getPlanMakeTime() { return planMakeTime; } public void setPlanMakeTime(Date planMakeTime) { this.planMakeTime = planMakeTime; } public int getPlanState() { return planState; } public void setPlanState(int planState) { this.planState = planState; } public int getState() { return state; } public void setState(int state) { this.state = state; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } public Set<CheckWorkRecordTemplate> getCheckWorkRecordTemplate() { return checkWorkRecordTemplate; } public void setCheckWorkRecordTemplate( Set<CheckWorkRecordTemplate> checkWorkRecordTemplate) { this.checkWorkRecordTemplate = checkWorkRecordTemplate; } public Set<CheckWorkRecord> getCheckWorkRecord() { return checkWorkRecord; } public void setCheckWorkRecord(Set<CheckWorkRecord> checkWorkRecord) { this.checkWorkRecord = checkWorkRecord; } }
三.表结构
四.配置映射文件
user.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- version: 1.0 date: 2013-06-06 author: 常宝龙 --> <hibernate-mapping package="com.inpoup.itoma.entity.user"> <!-- 人员表 --> <class name="User" table="ITYW_USER"> <id name="id" column="ID"> <generator class="sequence"> <param name="sequence">SEQ_ITYW_USER</param> </generator> </id> <property name="account" type="java.lang.String"> <column name="ACCOUNT" ></column> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" ></column> </property> <property name="name" type="java.lang.String"> <column name="NAME" ></column> </property> <property name="sex" type="java.lang.String"> <column name="SEX" ></column> </property> <property name="tel" type="java.lang.String"> <column name="TEL" ></column> </property> <property name="email" type="java.lang.String"> <column name="EMAIL" ></column> </property> <property name="depart" type="java.lang.String"> <column name="DEPART" ></column> </property> <property name="company" type="java.lang.String"> <column name="COMPANY" ></column> </property> <property name="state" type="java.lang.Integer"> <column name="STATE" ></column> </property> <set name="makeCheckWorks" inverse="true"> <key column="PLAN_MAKER"></key> <one-to-many class="com.inpoup.itoma.entity.work.CheckWork" /> </set> <!-- 人员表与巡检工作表的多对多关系 --> <set name="actionCheckWorks" table="CHECKWORK_OPERATOR"> <key column="USER_ID"></key> <many-to-many class="com.inpoup.itoma.entity.work.CheckWork" column="CHECKWORK_ID"></many-to-many> </set> <set name="makeDailyWorks" inverse="true"> <key column="PLAN_MAKER"></key> <one-to-many class="com.inpoup.itoma.entity.work.DailyWork" /> </set> <!-- 人员表与日常工作表的多对多关系 --> <set name="actionDailyWorks" table="DAILYWORK_OPEARTOR"> <key column="PLAN_MAKER"></key> <many-to-many class="com.inpoup.itoma.entity.work.DailyWork" column="DAILYWORK_ID"></many-to-many> </set> <set name="makeAssignWorks" inverse="true"> <key column="PLAN_MAKER"></key> <one-to-many class="com.inpoup.itoma.entity.work.AssignWork" /> </set> <!-- 人员表与个人工作表的多对多关系 --> <set name="personalWorks" table="PERSONALWORK_OPERATOR"> <key column="USER_ID"></key> <many-to-many class="com.inpoup.itoma.entity.work.PersonalWork" column="PERSONALWORK_ID"></many-to-many> </set> <!-- 人员表与角色表的多对多关系 --> <set name="roles" table="USER_ROLE"> <key column="USER_ID"></key> <many-to-many class="com.inpoup.itoma.entity.user.Role" column="ROLE_ID"></many-to-many> </set> <set name="checkWorkRecordTemplates" inverse="true"> <key column="TEMPLATE_MAKER"></key> <one-to-many class="com.inpoup.itoma.entity.work.CheckWorkRecordTemplate" /> </set> <set name="checkWorkRecords" inverse="true"> <key column="RECORD_USER_ID"></key> <one-to-many class="com.inpoup.itoma.entity.work.CheckWorkRecord" /> </set> <set name="dailyWorkRecords" inverse="true"> <key column="RECORD_USER_ID"></key> <one-to-many class="com.inpoup.itoma.entity.work.DailyWorkRecord" /> </set> <set name="dailyWorkRecordTemplates" inverse="true"> <key column="TEMPLATE_MAKER"></key> <one-to-many class="com.inpoup.itoma.entity.work.DailyWorkRecordTemplate" /> </set> </class>
work.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- version: 1.0 date: 2013-06-07 author: 常宝龙 --> <hibernate-mapping package="com.inpoup.itoma.entity.work"> <!-- 巡检工作表 --> <class name="CheckWork" table="CHECK_WORK"> <id name="id" column="ID"> <generator class="sequence"> <param name="sequence">SEQ_CHECK_WORK</param> </generator> </id> <property name="planStartTime" type="java.util.Date"> <column name="PLAN_START_TIME"></column> </property> <property name="planEndTime" type="java.util.Date"> <column name="PLAN_END_TIME"></column> </property> <property name="operationStartTime" type="java.lang.String"> <column name="OPERATION_START_TIME"></column> </property> <property name="operationEndTime" type="java.lang.String"> <column name="OPERATION_END_TIME"></column> </property> <property name="workCycle" type="java.lang.String"> <column name="WORK_CYCLE" ></column> </property> <property name="workDay" type="java.lang.String"> <column name="WORK_DAY" ></column> </property> <property name="cusRemind" type="java.lang.String"> <column name="CUS_REMIND" ></column> </property> <property name="remindTimes" type="java.lang.Integer"> <column name="REMIND_TIMES" ></column> </property> <property name="remindCycle" type="java.lang.Integer"> <column name="REMIND_CYCLE" ></column> </property> <property name="workCont" type="java.lang.String"> <column name="WORK_CONT" ></column> </property> <many-to-one name="user" column="PLAN_MAKER" class="com.inpoup.itoma.entity.user.User"> </many-to-one> <property name="planMakeTime" type="java.util.Date"> <column name="PLAN_MAKE_TIME"></column> </property> <property name="planState" type="java.lang.Integer"> <column name="PLAN_STATE" ></column> </property> <property name="state" type="java.lang.Integer"> <column name="STATE" ></column> </property> </class>
五。保存实例
package com.inpoup.itoma.service.work.impl; import java.text.SimpleDateFormat; import java.util.Date; import com.inpoup.itoma.dao.work.CheckWorkDao; import com.inpoup.itoma.entity.user.User; import com.inpoup.itoma.entity.work.CheckWork; import com.inpoup.itoma.service.work.CheckWorkService; import com.inpoup.web.core.dao.BaseDao; import com.inpoup.web.core.service.impl.BaseServiceImpl; /** * 巡检工作Service实现类 * @author 常宝龙 * @since 2013-06-13 * @version 1.0 * */ public class CheckWorkServiceImpl extends BaseServiceImpl<CheckWork> implements CheckWorkService{ public CheckWorkServiceImpl(BaseDao<CheckWork> baseDao) { super(baseDao); } private CheckWorkDao checkWorkDao; /** * 制定巡检工作计划 */ @Override public String makeCheckWork(Date planStartTime, Date planEndTime, String operationStartTime, String operationEndTime, String workCycle, String workDay, String cusRemind, String remindTimes, String remindCycle, String workCont, String makeId, String planMakeTime, String planState, String state,String assignPerson) throws Exception{ //时间转换 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); CheckWork checkWork = new CheckWork(); User user = new User(); //System.out.println("============================="+planStartTime); checkWork.setPlanStartTime(planStartTime); checkWork.setPlanEndTime(planEndTime); checkWork.setOperationStartTime(operationStartTime); checkWork.setOperationEndTime(operationEndTime); checkWork.setWorkCycle(workCycle); checkWork.setWorkDay(workDay); checkWork.setCusRemind(cusRemind); checkWork.setRemindTimes(Integer.parseInt(remindTimes)); checkWork.setRemindCycle(Integer.parseInt(remindCycle)); checkWork.setWorkCont(workCont); checkWork.setPlanMakeTime(simpleDateFormat.parse(planMakeTime)); checkWork.setPlanState(Integer.parseInt(planState)); checkWork.setState(Integer.parseInt(state)); user.setId(Integer.parseInt(assignPerson)); user.getMakeCheckWorks().add(checkWork); checkWork.getUsers().add(user); user.setId(Integer.parseInt(makeId)); checkWork.setUser(user); // //user.setId(Integer.parseInt(makeId)); // //解析分配执行人ID // //user.setId(Integer.parseInt(assignPerson[i])); // //assignPerson[i] // user.getMakeCheckWorks().add(checkWork); // checkWork.getUsers().add(user); // checkWork.setUser(user); //this.getCheckWorkDao().save(checkWork); this.addService(checkWork); return "success"; } public CheckWorkDao getCheckWorkDao() { return checkWorkDao; } public void setCheckWorkDao(CheckWorkDao checkWorkDao) { this.checkWorkDao = checkWorkDao; } }
相关推荐
在Java的持久化框架Hibernate中,多对一(ManyToOne)关联关系是一种常见的对象关系映射(ORM)场景。这种关系通常出现在一个实体类拥有多条与另一个实体类相关的记录,而另一个实体类可能只有一条对应的记录。例如...
本主题将主要聚焦在一对一和一对多关联。 2. **关联的方向性:** 关联可以是单向或双向的。单向关联只能从一个实体访问另一个,而双向关联允许两个实体之间互相访问。 3. **@OneToOne, @OneToMany, @ManyToOne ...
【标题】:“Hibernate多对一关联demo” 在Java开发中,Hibernate是一个强大的对象关系映射(ORM)框架,它简化了数据库操作。本教程将详细解释如何在Hibernate中实现多对一的关联关系,以及如何通过一个实际的示例...
### Hibernate映射一对多关联关系 #### 知识点概览 - **概念解析**:一对多(One-to-Many)关联关系的概念及其在Hibernate中的实现方式。 - **域模型设计**:创建具有一个实体与多个实体关联的域模型。 - **关系...
在实体类之间,Hibernate支持多种关联关系,包括一对一(One-to-One)、一对多(One-to-Many)和多对多(Many-to-Many)。本资源主要探讨的是“hibernate一对一关联关系”。 一对一关联关系在现实世界中很常见,...
这篇博客文章“hibernate一对多关联映射(单向关联)”将深入讲解如何配置和使用这种映射关系。 在单向关联中,只有一个实体知道另一个实体的存在,也就是说,只有父实体("一"的一端)有对子实体("多"的一端)的...
"hibernate_many2many_2"这个文件名可能是指一个关于Hibernate多对多关联的示例或教程的第二部分,可能包含配置文件、Java实体类、映射文件以及相关的测试代码。 **详细知识点:** 1. **实体类定义**:在Hibernate...
在Java的持久化框架Hibernate中,"多对一"关联关系是一种常见的对象关系映射(ORM)设计,它表示一个实体(子项)可以与多个其他实体(父项)相关联,而每个父项可能被多个子项引用。在数据库层面,这通常对应于一个...
在这个“Hibernate一对多双向自身关联demo代码”中,我们将深入理解如何实现一个实体类与其自身进行一对多的双向关联。这种关联在实际应用中常见于例如用户的朋友关系、员工的上下级关系等场景。 首先,让我们了解...
本教程“传智播客hibernate教程_保存多对一关联关系的更进一步分析”深入探讨了如何在Hibernate中处理多对一的关联关系。这种关联意味着一个实体(多方)可以与多个其他实体(一方)相关联,而每个其他实体只与一个...
本项目“hibernate一对多项目”旨在演示如何在JavaWeb应用中使用Hibernate处理一对多的关系映射。这里我们将深入探讨 Hibernate 的一对多关系、配置以及在实际项目中的应用。 首先,一对多关系在数据库中很常见,...
在Java的持久化框架Hibernate中,双向一对多关联映射是一种常见的关系映射方式,它模拟了数据库中的外键关联,使得一个实体可以拥有多个另一个实体的实例。在这个注解版的实现中,我们将深入探讨如何使用Hibernate的...
在Java的持久化框架Hibernate中,一对一(OneToOne)关系是一种常见的对象关系映射(ORM)配置,用于表示两个实体类之间的一种唯一关联。在这个场景中,“hibernate使用主键关联的一对一关系”指的是通过共享主键来...
本主题聚焦于“Hibernate双向一对一关联映射”的注解实现,这是一种高级的数据库设计模式,用于处理两个实体之间一对一的关系。 在Hibernate中,一对一关联映射分为单向和双向。单向一对一映射通常涉及一个实体持有...
标题"Hibernate一对一主键关联映射(双向关联)"中的"主键关联"指的是两个实体通过共享相同的主键来建立关联。"双向关联"则意味着在两个实体类中,都可以直接访问到对方的实例,即在实体A中有一个对实体B的引用,...
1. **一对多关联**(Many-to-One) - 在这种关系中,一个实体可以对应多个其他实体。例如,一个班级可以有多个学生,而每个学生只属于一个班级。 - **注解配置**:在Java类中,使用`@ManyToOne`注解标记字段,同时...
一对多关联是指在数据库中,一个表的记录可以与另一个表中的多个记录相关联,这种关系在Hibernate中通过`@OneToMany`注解来表示。例如,一个用户可以有多个订单,那么用户实体(User)与订单实体(Order)之间就存在...
本篇文章将详细讲解"hibernate一对多与多对一"的关系映射概念,以及如何在实际开发中进行配置和使用。 首先,我们来看一下“一对多”关系。在现实世界中,这种关系可以对应到例如一个班级有多个学生,或者一个人...
本文将详细解析" Hibernate一对多关联实例 ",并涵盖关联的保存(save)、删除(delete)、查询(select)和更新(update)操作。 一、一对多关联概念 在数据库设计中,一对多关联表示一个表中的记录可以与另一个表中的多个...
本示例主要关注的是Hibernate中的多对多关联映射及其添加与查询方法。 多对多关联是数据库设计中的一种常见关系类型,指的是两个实体之间可以存在多个对应关系。例如,在学生与课程的关系中,一个学生可以选修多门...