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

Hibernate关联关系映射

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

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.net/115516.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 手机和电脑将XPS转换成PDF的免费方法

    手机和电脑将XPS转换成PDF的免费方法如何将XPS转成PDF?最近有小伙伴收到很多后缀为XPS的文件,不知道如何打开,更不懂怎么样使用,如果能转成常用的PDF格式就好了。打开XPS文件最简单的方法当然还是安装对应的阅读器,比如XPSViewer;除此之外其实很多PDF阅读器也是可以直接打开XPS文档的,比如极速PDF阅读器。用以上任意一种方式打开XPS文件后,利用PDF生成的其中方式之一,将文件进行打印并选择PDF虚拟打印机就能直接将XPS文件以PDF格式进行保存了。这种方式一般适用于电脑端,毕竟手机无法进行这种虚拟打印机的操作。但如

    2022年5月4日
    49
  • C语言实现学生成绩管理系统设计

    C语言实现学生成绩管理系统设计本系统有**增加学生记录、修改学生记录、删除学生记录、按姓名查询学生记录、按C语言成绩对学生进行排序、退出系统**6大功能。能够对学生的姓名,学号,c语言成绩做相应的操作。在检测到输入成绩大于55时,会自动加上5。该管理系统设计功能模块图:下面是源代码:#include”stdio.h”#include”string”/*定义学生结构体*/structStudent

    2022年6月20日
    27
  • HTML iframe 标签[通俗易懂]

    HTML iframe 标签[通俗易懂]定义和用法iframe元素会创建包含另外一个文档的内联框架(即行内框架)把需要的文本放置在和之间,这样就可以应对无法理解iframe的浏IFrame对象IFrame对象代表一个HTML的内联框架在HTML文档中每出现一次,一个IFrame对象就会被创建。属性frameborder=1或0规定是否显示边框height=pixels或%…

    2025年7月8日
    2
  • android 电脑浏览器,这5种轻量级的国内Android浏览器,都有自己的“专有秘密”…

    android 电脑浏览器,这5种轻量级的国内Android浏览器,都有自己的“专有秘密”…在上一篇文章中,我整理并测试了六种来自国际力量的Android浏览器,包括流行的Chrome,具有丰富扩展功能的Firefox,具有隐私和安全性的FirefoxFocus,具有悠久历史的Opera,以及后来的MicrosoftEdge,Yandex除了主要国际制造商生产的产品外,国内开发商还致力于Android浏览器类别。他们希望在保持简单设计的基础上,寻求提供更多差异化的功能。这次我选择了五…

    2022年5月15日
    167
  • offsetWidth、clientWidth、width、scrollWidth区别及获取

    offsetWidth、clientWidth、width、scrollWidth区别及获取&lt;!DOCTYPEhtml&gt;&lt;htmllang="en"&gt;&lt;head&gt;&lt;metacharset="UTF-8"&gt;&lt;title&gt;offsetWidth、clientWidth、width、scrollWidth区别及获取&lt;/title&gt;&lt;style

    2022年7月22日
    12
  • HikariPool配置详解

    HikariPool配置详解HikariPool较佳配置 hikari: connection-timeout:60000 validation-timeout:3000 idle-timeout:60000 login-timeout:5 max-lifetime:60000 maximum-pool-size:10 minimum-idle:10 read-only:falsehikari各参数解释https://github.com/.

    2022年6月23日
    445

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号