hibernate ORM related
一、单向关联(unidirectional associations):
1.1.1 Many-to-one
Employee.hbm.xml
<class name="Employee" table="T_EMPLOYEE">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
<many-to-one name="department" column="DEPARTMENT_ID" not-null="true"/>
</class>
Department.hbm.xml
<class name="Department" table="T_DEPARTMENT"> <id name="department_Id" column="DEPARTMENT_ID"> <generator class="native"/> </id> </class> create table EMPLOYEE(employee_Id bigint not null primary key,department_Id bigint not null) create table DEPARTMENT(department_Id bigint not null primary key)
?单向的<many-to-one>里没有指明class
1.1.2 One-t-one
1>单向一对一和单向多对一区别就是Unique这个属性,基于外键
<class name="Employee" table="T_EMPLOYEE">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
<many-to-one name="department"
column="DEPARTMENT_ID"
unique="true"
not-null="true"/>
</class> <class name="Department" table="T_DEPARTMENT">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
</class>
2>基于主键
<class name="Person" table="Person">
<id name="Person_Id" column="PERSON_ID">
<generator class="native"/>
</id>
</class> <class name="Card" table="Card">
<id name="Card_Id" column="PERSON_ID">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" constrained="true"/>
</class> create table Person ( PERSON_ID bigint not null primary key )
create table Card( PERSON_ID bigint not null primary key )
? one-to-one 没有column和table属性,有class属性,name标志?,如何指定person对象。
1.1.3 One-to-many
<class name="Department">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
<set name="employee">
<key column="DEPARTMENT_ID"
not-null="true"/>
<one-to-many class="Employee"/>
</set>
</class> <class name="Employee">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
</class> create table Department( department_Id bigint not null primary key )
create table Emplyee( employee_Id bigint not null primary key, personId bigint not null )
you should instead use a join table for this kind of asociation
二、单向关联with join table
2.1.1 One-to-many
<class name="Department">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
<set name="employees" table="DepartmetEmployee">
<key column="DEPARTMENT_ID"/>
<many-to-many column="EMPLOYEE_ID"
unique="true"
class="Employee"/>
</set>
</class> <class name="Employee">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
</class> create table Department( Department_Id bigint not null primary key )
create table DepartmentEmployee(Department_Id bigint not null, employee_Id bigint not null primary key )
create table Employee( employee_Id bigint not null primary key )
specifying unique="ture" change multiplicity from many-to-many to one-to-many.
2.1.2.Many-to-one
<class name="Department">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
</class> <class name="Employee">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
<join table="EmployeeDepartment"
optional="true">
<key column="EMPLOYEE_ID" unique="true"/>
<many-to-one name="department"
column="DEPARTMENT_ID"
not-null="true"/>
</join>
</class>
2.1.3 One-to-one
<class name="Person" table="Person">
<id name="Person_Id" column="PERSON_ID">
<generator class="native"/>
</id>
<join table="PersonCard"
optional="true">
<key column="PERSON_ID"
unique="true"/>
<many-to-one name="card"
column="CARD_ID"
not-null="true"
unique="true"/>
</join>
</class> <class name="Card" table="Card">
<id name="Card_Id" column="CARD_ID">
<generator class="native"/>
</id>
<one-to-one name="person" constrained="true"/>
</class> create table Person ( PERSON_ID bigint not null primary key )
create table PersonCard ( PERSON_ID bigint not null primary key, CARD_ID bigint not null unique )
create table Card( CARD_ID bigint not null primary key )
2.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> create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )
三、双向关联(Bidirectional associations)
3.1.1.one-to-one
1>
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"/>
</class> <class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person"
constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )
2>
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class> <class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class> create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )
3.1.2.one-to-many/many-to-one:
1>
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class> <class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="people" inverse="true">
<key column="addressId"/>
<one-to-many class="Person"/>
</set>
</class> create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )
2>
<class name="Person">
<id name="id"/>
...
<many-to-one name="address"
column="addressId"
not-null="true"
insert="false"
update="false"/>
</class> <class name="Address">
<id name="id"/>
...
<list name="people">
<key column="addressId" not-null="true"/>
<list-index column="peopleIdx"/>
<one-to-many class="Person"/>
</list>
</class>
四、双向关联with join table
4.1.1.one-to-many/many-to-one
4.1.2.one-to-one
4.1.3.many-to-many
hibernate ORM related的更多相关文章
- wildfly 10上使用最新的 Hibernate ORM OGM
ORM是关系型数据库连接:ogm是No sql数据库连接,Mongo, redis等. 1,下载ogm zip包,解压到wildfly_home\modules\system\layers\base, ...
- [JavaEE] Hibernate ORM
一. Hibernate的简要介绍 Hibernate是轻量级Java EE应用的持久层解决方案,Hibernate不仅管理者Java类到数据库表的映射(包括Java 数据类型到SQL数据类型的映射) ...
- Hibernate (ORM)
1 框架体系结构 2 hibernate入门 2.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBean Relational: ...
- Spring ORM数据訪问——Hibernate
Hibernate 我们将首先介绍Spring环境中的Hibernate 5.然后介绍使用Hibernate 5来演示Spring集成O-R映射器的方法. 本节将具体介绍很多问题,并显示DAO实现和事 ...
- Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide
Preface Validating data is a common task that occurs throughout all application layers, from the pre ...
- JSP利用Hibernate实现对数据库的CRUD ——开发环境Myeclipse与SQL Server 2008
一.首先先建立一个Web Project 二.然后在程序根目录建立文件夹“DataBase”和“Doc”,分别存放数据库文件和保存SQL语句,建完如下所示: 三.建立数据库“dbHibernate”, ...
- hibernate入门实例
1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...
- hibernate(1)
1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...
- 如何下载Hibernate
官网: http://hibernate.org/ 打开hibernate官网,选择Hibernate ORM,点击左侧的Downloads 点击Downloads后,可以看到如下页面,右侧是各个版本 ...
随机推荐
- App的开发过程
不同的项目管理模式或许会有完全不同的流程步骤.但是专业性几乎是保证产品质量的唯一准则. App的开发过程主要分为以下阶段,本文会按顺序为大家简单地说明: 1.需求梳理.分析 2.产品原型图绘制 3.U ...
- ASP.NET,什么是MVC,MVC的简单介绍
什么是MVC模式 MVC(Model-View-Controller,模型—视图—控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Co ...
- 在GCE上安装Apache、tomcat等
1.安装Apache2.2.3 (虚机的操作系统是CentOS7) sudo yum install wget -y cd /opt sudo wget http://archive.apache.o ...
- django-admin 登录之后显示页面,表是否显示
如果是超级用户可以全部看到(如图),如果是普通用户,只能看到user与group 虽然实现了其功能,不过有些地方没搞懂,所以有些地方出了写的不好 ```class PermissionsMixin(m ...
- 机器学习算法之:KNN
基于实例的学习方法中,最近邻法和局部加权回归法用于逼近实值或离散目标函数,基于案例的推理已经被应用到很多任务中,比如,在咨询台上存储和复用过去的经验:根据以前的法律案件进行推理:通过复用以前求解的问题 ...
- Python编程-函数进阶
一.函数对象 函数是第一类对象,即函数可以当作数据传递 1 可以被引用 2 可以当作参数传递 3 返回值可以是函数 4 可以当作容器类型的元素 def foo(): print('from foo') ...
- SQL单行函数和多行函数
单行函数和多行函数示意图: 单行函数分为五种类型:字符函数.数值函数.日期函数.转换函数.通用函数 单行函数: --大小写控制函数 select lower('Hello World') 转小写, u ...
- 20165101刘天野 2017-2018-2 《Java程序设计》第3周学习总结
20165101刘天野 2017-2018-2 <Java程序设计>第3周学习总结 编程语言的几个发展阶段 类 构造方法与对象的创建 类与程序的基本结构 参数传值 对象的组合 实例成员与类 ...
- 在unity 中,使用http请求,下载文件到可读可写路径
在这里我用了一个线程池,线程池参数接收一个带有object参数的,无返回值的委托 ,下载用到的核心代码,网上拷贝的,他的核心就是发起一个web请求,然后得到请求的响应,读取响应的流 剩下的都是常见的I ...
- JAVAWeb学习总结(3)
JavaWeb学习总结(三)——Tomcat服务器学习和使用(二) 一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命 ...