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

Hibernate关联关系映射的更多相关文章

  1. Hibernate关联关系映射之一对一关联关系

    人和身份证之间就是一个典型的一对一关联关系.实现一对一关联关系映射的方式有两种一种是基于外键,一种是基于主键,下面我们先看基于外键的关联方式 首先看他们的实体类 Person类 ? 1 2 3 4 5 ...

  2. hibernate关联关系映射详解

    词汇解释 关系:事物之间相互作用.相互联系的状态.范围最大. 联系:在关系数据库中表示实体与实体之间的联系,1:1,1:n,m:n. 关联:表示对象之间的关系,既有数量性,又有方向性:动词:将对象之间 ...

  3. hibernate关联关系映射之配置文件

    词汇解释 关系:事物之间相互作用.相互联系的状态.范围最大. 联系:在关系数据库中表示实体与实体之间的联系,1:1,1:n,m:n. 关联:表示对象之间的关系,既有数量性,又有方向性:动词:将对象之间 ...

  4. Hibernate关联关系映射之一对多双向映射

    一对多映射有两种,一种是单向的,另一种的多向.我们一般是使用双向的,所以我就写写一对多的双向映射. 还是想昨天一样举个例子来说明:作者<===>作品,还是对数据进行增删改查. 我们一般是把 ...

  5. Hibernate关联关系映射之一对一(主键关联)

    在业务成的域模型中,类和类之间最普遍的关系就是关联关系,而关联也是有方向的. 就以例子来说明:一个人对应一张身份证.对其进行增删改. 对于人在数据创建表的时候,我们就给他两个字段,一个是id号,一个就 ...

  6. Hibernate学习笔记(3)---hibernate关联关系映射

    一对一关联 假设有两个持久化类(实体类)User与Address,它们之间存在一对一的关系 1,通过主键关联(个人偏向另外一种) User.hbm.xml文件配置 <id name=" ...

  7. 1-7 hibernate关联关系映射

    1.关联关系分为单向关联(一对一,一对多,多对一,多对多),多向关联(一对一,一对多,多对多). 2.单向一对一主键关联实例 需要为one-to-one元素指定constrained属性值为true. ...

  8. hibernate学习四 hibernate关联关系映射

    在Hibernate中对象之间的关联关系表现为数据库中表于表之间的关系(表之间通过外键关联). 1 单向的一对一 主键关联  外键关联 2 单向的一对多 3 单向的多对一 4 单向的多对多 5 双向的 ...

  9. Hibernate 关联关系映射实例

    双向多对一/一对多(many-to-one/one-to-many) 例子,多个学生对应一个班级,一个班级对应多个学生: 班级类,Grade.java: public class Grade { pr ...

随机推荐

  1. IndexReader和IndexWriter的生命周期

    http://youyang-java.iteye.com/blog/1731205 对于IndexReader而言,反复使用 IndexReader .open打开会有很大的开销,所以一般在整个程序 ...

  2. Cygwin环境编译/usr/include/sys/_types.h:72:20: 致命错误:stddef.h:can not found

    环境介绍: win7_x64 +Cygwin64 gcc :4.8.2 g++:4.8.1 编译 c++的helloworld.cpp 一直失败! 代码如下: #include <iostrea ...

  3. 【转】DCX (数独-八皇后问题)

    还没学会,先转了再说.. 出处:http://grenet.cnblogs.com/ 跳跃的舞者,舞蹈链(Dancing Links)算法--求解精确覆盖问题   精确覆盖问题的定义:给定一个由0-1 ...

  4. 131. Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  5. springboot 配置多数据源

    1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...

  6. [置顶] *p++/*++p区别-linux

    #include <stdio.h> main() { char * s = "123456"; char * p; p = s; printf( "%c\n ...

  7. Java SE知识点

    Java概述 Java的运行机制 JDK,JRE,JVM Java开发环境搭建 用记事本编写运行一个Hello World的完整过程 如何在Java中使用注释 使用Eclipse开发Java程序 使用 ...

  8. WordPress Contact Form 7插件任意文件上传漏洞

    漏洞名称: WordPress Contact Form 7插件任意文件上传漏洞 CNNVD编号: CNNVD-201311-415 发布时间: 2013-11-28 更新时间: 2013-11-28 ...

  9. epub3 in action: epub3文件格式简介

    epub3文件就是一个符合epub3标准,以epub为扩展名的zip压缩文件.epub3标准则是基于html5.css3.svg等web标准以及mathML等来展示内容.下图就是一个简单epub3文件 ...

  10. GTK+系统中的对话框(GTK+dialogs)

    GTK+系统中的对话框(GTK+dialogs) GTK+系统中的对话框(GTK+ dialogs) 在接下来的章节中我们将着重介绍GTK+系统中的对话框. 对话框窗口是众多GUI应用程序中不可或缺的 ...