首页 > 其他分享 >Hibernate关联关系映射

Hibernate关联关系映射

时间:2023-04-23 11:01:14浏览次数:29  
标签:DEPTID Hibernate 映射 关联 _. session SCOTT transaction


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");
 
"就业部");
"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

 

标签:DEPTID,Hibernate,映射,关联,_.,session,SCOTT,transaction
From: https://blog.51cto.com/u_6784072/6216868

相关文章

  • Django 知识库:path()路径映射
    网站地址是由统一资源定位符表示的,也是就我们常说的url。Django中有非常强大的path()方法,可以动态构造出你想要的各种不同形态的url。基本写法如下:fromdjango.urlsimportpathurlpatterns=[#固定地址path('articles/2003/',...),#可传入int参......
  • map:映射库
    #include<map>usingnamespacestd;map<string,int>m1;//定义一个空的map,键类型为string,值类型为intmap<string,int>m2={{"apple",1},{"banana",2},{"orange",3}};//使用花括号进行初始化map<string,int>m3(m2);//使用拷贝构造......
  • EasyExcel格式化映射注解和样式注解详解
     https://blog.csdn.net/qq_44749491/article/details/127879946一、概述使用注解很简单,只要在对应的实体类上面加上注解即可。也就是说使用实体类模型来读写Excel文件时,可以通过注解来控制实体类字段和Excel列之间的对应关系。二、ExcelProperty2.1作用ExcelProperty注解用......
  • 深度学习--统计与数据映射
    深度学习--统计与数据映射范数importtorch#范数norm第一范数:绝对值求和第二范数:平方和后求根号norm使用要求是浮点数a=torch.full([8],1.)#tensor([1.,1.,1.,1.,1.,1.,1.,1.])b=a.view(2,4)#tensor([[1.,1.,1.,1.],#[1.,1.,1.,......
  • drf之多表关联反序列化保存read_only与write_only
    目录read_only与write_only示例假如前端传入了一组数据:{name:'赛尔达传说:王国之泪',price:350,publish:1,authors:[1,2]}如上:publish按id传入,authors也按id传入。read_only与write_onlyread_only用于序列化write_only用于反序列化这两个是字段参数示例#......
  • hibernate:分页
    intpage=2;//分页页码 intsize=2;//列数 List<User>users=session.createQuery("selectafromUsera") .setFirstResult((page-1)*size)//起始条数startend .setMaxResults(size)//列数 .list(); //select*fromuserlimit0,2起始条数,列数 for......
  • JMeter入门教程(11) --关联
    文章目录1.任务背景2.任务目标3.任务实操1.任务背景当JMeter执行脚本时,伪装成浏览器,然后根据脚本,把当初真的浏览器所发过的内容,再对网站服务器重新发送一遍,JMeter企图骗过服务器,让服务器以为它就是当初的浏览器,然后把网站内容传送给JMeter。2.任务目标掌握掌握JMeter性能测试脚本......
  • loadrunner入门教程(17) --关联
    文章目录1.任务背景2.任务目标3.任务实操1.任务背景当执行脚本时,VuGen伪装成浏览器,然后根据脚本,把当初真的浏览器所发过的内容,再对网站服务器重新发送一遍,VuGen企图骗过服务器,让服务器以为它就是当初的浏览器,然后把网站内容传送给VuGen。所以记录在脚本中要对服务器所发送的内......
  • 数据库关联查询(左连接、右连接)
    在项目中用到多表联合查询,发现2个现象,今天解决这2个疑问:1、left join连接2张表,on后的条件第一个生效,用and连接的其他条件不生效;2、一旦加上where,则显示的结果等同于inner join;先写结论:过滤条件放在:where后面:是先连接然生成临时查询结果,然后再筛选on后面:先根据条件过滤筛选......
  • Redis - 数据类型映射底层结构
    简介从数据类型上体现就是,同一个数据类型,在不同的情况下会使用不同的编码类型,底层所使用的的数据结构也不相同。字符串对象字符串对象的编码可以是int、raw和embstr三者之一。embstr编码是专门用于保存简短字符串的一种优化编码方式,与raw编码会调用两次内存分配函数分......