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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • GB28181服务器_GB28181收费吗

    GB28181服务器_GB28181收费吗CarEye开发GB28181服务器有将近两年时间了,早期我们用纯C++开发了一个GB28181视频服务期,对外的接口是基于MQ协议的。这样开发出来的服务器主要有几个问题。1.SIP服务器和流媒体服务器是绑定在一个进程中的,因为没有分离,造成了视频处理和SIP服务器只能在一台服务器上运行,既不能打到GB28181协议的构架要求。也造成无法使用负载均衡的功能。2.对外接口采用了MQ通信方式。虽然MQ消息处理实时,对一些数据处理,如报警,对讲等。但MQ本身是重量级构建,不方便一些应用场景快速构建

    2022年8月31日
    0
  • NHibernate 进阶

    NHibernate 进阶  在我的最近文章中,我介绍了 Nhibernate。在这里和在其他的论坛中 , 因为我没有强调NHibernate 只是许多可得的 ORM 解决方案之一,(事实上,现在对.NET开发者来说,开源的加上商业的 ORM 的架构现在是超过 50个可供选择)。 作为一个开发顾问,我会经常用到Hibernate(大家都知道它吧)既然我必须在.NET平台下选择一个,NHibernate是我最明智的选择。 我

    2022年7月14日
    14
  • ubuntu安装vscode的两种方法_linux vscode

    ubuntu安装vscode的两种方法_linux vscode1、vscode官网下载.deb文件:https://code.visualstudio.com/解决Vscode下载慢的问题官网的下载链接,替换az764295.vo.msecnd.net为vscode.cdn.azure.cn例如:原始下载链接:https://az764295.vo.msecnd.net/stable/3a6960b964327f0e3882ce18fcebd07ed191b316/code_1.62.2-1636665017_amd64.deb替换为:https://

    2022年9月18日
    0
  • WebSocket快速上手

    WebSocket快速上手文章目录WebSocket快速上手1.WebSocket是什么?1.1.WebSocket连接过程1.2.WebSocket与HTTP对比1.3.WebSocket协议2.快速上手2.1服务端2.2客户端2.2.1浏览器客户端2.2.2Java客户端WebSocket快速上手1.WebSocket是什么?WebSocket建立在TCP协议之上,并且与HTTP协议有着良好的…

    2022年7月27日
    1
  • generic host process for win32_weblogic kernel.default

    generic host process for win32_weblogic kernel.default classWin32_NetworkAdapterConfiguration:CIM_Setting{booleanArpAlwaysSourceRoute;booleanArpUseEtherSNAP;stringCaption;stringDatabasePath;booleanDeadGWDetectEnabl

    2022年10月2日
    0
  • vue前端跨域解决方案有哪些_前端能完全解决跨域问题吗

    vue前端跨域解决方案有哪些_前端能完全解决跨域问题吗为什么会出现跨域:浏览器访问非同源的网址时,会被限制访问,出现跨域问题.常见的跨域有三种:jspn跨域,原理:动态生成script标签,通过script标签引入接口地址(因为script标签不存在跨域的)cors跨域(后端开启):全称“跨域资源共享”,原理:它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制vue代理服务器proxy跨域:通过请求本地的服务器,然后本地的服务器再去请求远程的服务器(后端部署接口的服务器),最后本地服务器再将请求

    2022年10月1日
    1

发表回复

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

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