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)
上一篇 2022年2月3日 下午12:00
下一篇 2022年2月3日 下午12:00


相关推荐

  • poj 3237 Tree(树链拆分)

    poj 3237 Tree(树链拆分)

    2022年1月3日
    47
  • python连接数据库插入数据「建议收藏」

    python连接数据库插入数据「建议收藏」python连接数据库插入数据在数据库创建表并插入测试数据dropdatabaseifexistshrs;createdatabasehrsdefaultcharsetutf8mb4;usehrs;createtabletb_dept(dnointnotnullcomment’编号’,dnamevarchar(10)notnullcomment’名称’,dlocvarchar(20)notnullcomment’所在地’,prim

    2025年7月12日
    4
  • 腾讯内测QClaw:一键部署「龙虾」OpenClaw,微信、QQ双端直连

    腾讯内测QClaw:一键部署「龙虾」OpenClaw,微信、QQ双端直连

    2026年3月12日
    1
  • 关于CPLD与FPGA的对比分析

    关于CPLD与FPGA的对比分析1.PLD/FPGA/CPLDPLD(ProgrammableLogicDevice):可编程逻辑器件,数字集成电路半成品,芯片上按照一定的排列方式集成了大量的门和触发器等基本逻辑元件,使用者按照设计要求运用开发工具将这些片内的元件连接起来,此过程称为编程;FPGA:基于查找表技术,要外挂配置用的EEPROM的PLD产品;由逻辑功能块排列为阵列,并由可编程的内部连线连接…

    2022年6月3日
    39
  • ManualResetEvent浅谈

    ManualResetEvent浅谈C#中ManualResetEvent的开关作用贴代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespacetest01{clas…

    2022年7月18日
    28
  • 多模型融合权重如何训练_单因子模型

    多模型融合权重如何训练_单因子模型本篇文章有别于传统的多因子研究,我们并未将重点放在阿尔法因子的挖掘上,而是通过对股票组合的权重优化计算,找到了在市值中性、行业中性、风格因子中性约束下的最优投资组合,以及验证得到的组合权重是否满足了约束条件。结构化多因子风险模型首先对收益率进行简单的线性分解,分解方程中包含四个组成部分:股票收益率、因子暴露、因子收益率和特质因子收益率。那么,第只股票的线性分解如下所示:rj=x1f1+x2f2+x3f3+x4f4⋅⋅⋅⋅xKfK+ujr_j=x_1f_1+x_2f_2+x_3f_3+x_4f_4···

    2022年10月6日
    4

发表回复

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

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