前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Hibernate关联关系映射[通俗易懂]

Hibernate关联关系映射[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-10 13:13:22
发布2022-07-10 13:13:22
5770
举报

大家好,又见面了,我是全栈君。

1. Hibernate关联关系映射

1.1. one to one

<class name=”Person”> <id name=”id” column=”personId”> <generator class=”native”/> </id> <join table=”PersonAddress” optional=”true”> <key column=”personId” unique=”true”/> <many-to-one name=”address” column=”addressId” not-null=”true” unique=”true”/> </join> </class> <class name=”Address”> <id name=”id” column=”addressId”> <generator class=”native”/> </id> </class>

1.2. one to many

<?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”> <!– Mapping file autogenerated by MyEclipse Persistence Tools –> <hibernate-mapping> <class name=”com.morris.hql.entity.Department” table=”DEPARTMENT” schema=”SCOTT”> <id name=”deptid” type=”java.lang.String”> <column name=”DEPTID” length=”20″ /> <generator class=”assigned” /> </id> <property name=”deptname” type=”java.lang.String”> <column name=”DEPTNAME” length=”20″ not-null=”true” /> </property> <set name=”employees” inverse=”true”> <key> <column name=”DEPTID” length=”20″ /> </key> <one-to-many class=”com.morris.hql.entity.Employee” /> </set> </class> </hibernate-mapping>

1.3. many to one

<?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”> <!– Mapping file autogenerated by MyEclipse Persistence Tools –> <hibernate-mapping> <class name=”com.morris.hql.entity.Employee” table=”EMPLOYEE” schema=”SCOTT”> <id name=”empid” type=”java.lang.String”> <column name=”EMPID” length=”20″ /> <generator class=”assigned” /> </id> <many-to-one name=”department” class=”com.morris.hql.entity.Department” fetch=”select”> <column name=”DEPTID” length=”20″ /> </many-to-one> <property name=”empname” type=”java.lang.String”> <column name=”EMPNAME” length=”20″ not-null=”true” /> </property> </class> </hibernate-mapping>

1.4. many to many

<class name=”Person”> <id name=”id” column=”personId”> <generator class=”native”/> </id> <set name=”addresses” table=”PersonAddress”> <key column=”personId”/> <many-to-many column=”addressId” class=”Address”/> </set> </class> <class name=”Address”> <id name=”id” column=”addressId”> <generator class=”native”/> </id> </class>

1.5. 实例

1.5.1. 级联添加

public void addDeptEmp(Department dept, Employee emp) { Session session = HibernateSessionFactory.getSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); dept.getEmployees().add(emp); emp.setDepartment(dept); session.save(dept); transaction.commit(); } catch (Exception e) { if (transaction != null) { transaction.rollback(); } e.printStackTrace(); } finally { if (session != null) { session.close(); } } }

打印的sql语句

Hibernate: select employee_.EMPID, employee_.DEPTID as DEPTID0_, employee_.EMPNAME as EMPNAME0_ from SCOTT.EMPLOYEE employee_ where employee_.EMPID=? Hibernate: insert into SCOTT.DEPARTMENT (DEPTNAME, DEPTID) values (?, ?) Hibernate: insert into SCOTT.EMPLOYEE (DEPTID, EMPNAME, EMPID) values (? , ? , ? )

1.5.2. 级联删除

public void deleteDeptEmp(Department dept, Employee emp) { Session session = HibernateSessionFactory.getSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); session.delete(dept); transaction.commit(); } catch (Exception e) { if (transaction != null) { transaction.rollback(); } e.printStackTrace(); } finally { if (session != null) { session.close(); } } }

打印的sql语句

Hibernate: select department0_.DEPTID as DEPTID1_1_, department0_.DEPTNAME as DEPTNAME1_1_, employees1_.DEPTID as DEPTID3_, employees1_.EMPID as EMPID3_, employees1_.EMPID as EMPID0_0_, employees1_.DEPTID as DEPTID0_0_, employees1_.EMPNAME as EMPNAME0_0_ from SCOTT.DEPARTMENT department0_ left outer join SCOTT.EMPLOYEE employees1_ on department0_.DEPTID=employees1_.DEPTID where department0_.DEPTID=? Hibernate: delete from SCOTT.EMPLOYEE where EMPID=? Hibernate: delete from SCOTT.DEPARTMENT where DEPTID=?

1.5.3. 级联改动

public void updateDeptEmp() { Session session = HibernateSessionFactory.getSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); Department dept = (Department) session.load(Department.class, “5”); Employee emp = (Employee) session.load(Employee.class, “1001”); dept.setDeptname(“就业部“); emp.setEmpname(“a”); session.update(dept); session.update(emp); transaction.commit(); } catch (Exception e) { if (transaction != null) { transaction.rollback(); } e.printStackTrace(); } finally { if (session != null) { session.close(); } } }

打印的sql语句

Hibernate: select department0_.DEPTID as DEPTID1_1_, department0_.DEPTNAME as DEPTNAME1_1_, employees1_.DEPTID as DEPTID3_, employees1_.EMPID as EMPID3_, employees1_.EMPID as EMPID0_0_, employees1_.DEPTID as DEPTID0_0_, employees1_.EMPNAME as EMPNAME0_0_ from SCOTT.DEPARTMENT department0_ left outer join SCOTT.EMPLOYEE employees1_ on department0_.DEPTID=employees1_.DEPTID where department0_.DEPTID=? Hibernate: select employee0_.EMPID as EMPID0_0_, employee0_.DEPTID as DEPTID0_0_, employee0_.EMPNAME as EMPNAME0_0_ from SCOTT.EMPLOYEE employee0_ where employee0_.EMPID=? Hibernate: update SCOTT.DEPARTMENT set DEPTNAME=? where DEPTID=? Hibernate: update SCOTT.EMPLOYEE set DEPTID=?, EMPNAME=? where EMPID=?

1.5.4. 级联查询

public Employee queryEmployeeById(String id){ Session session = HibernateSessionFactory.getSession(); Employee employee = null; try { employee = (Employee) session.load(Employee.class, id); System.out.println(employee.getEmpname()); } finally { if (session != null) { session.close(); } } return employee; }

打印的sql语句

Hibernate: select employee0_.EMPID as EMPID0_0_, employee0_.DEPTID as DEPTID0_0_, employee0_.EMPNAME as EMPNAME0_0_ from SCOTT.EMPLOYEE employee0_ where employee0_.EMPID=? Hibernate: select department0_.DEPTID as DEPTID1_0_, department0_.DEPTNAME as DEPTNAME1_0_ from SCOTT.DEPARTMENT department0_ where department0_.DEPTID=? a

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115516.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Hibernate关联关系映射
    • 1.1. one to one
    • 1.2. one to many
    • 1.3. many to one
    • 1.4. many to many
    • 1.5. 实例
      • 1.5.1. 级联添加
      • 1.5.2. 级联删除
      • 1.5.3. 级联改动
      • 1.5.4. 级联查询
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档