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后,可以看到如下页面,右侧是各个版本 ...
随机推荐
- 剑指offer 面试68题
面试68题: 题目:求树中两个节点的最低公共祖先 待解决...
- key points & (QA) about RPKI
@1: Q: What does ROA look like?Since ROA means which ASes are allowed for originating routes to some ...
- ModelForm组件介绍
照抄自:http://www.jb51.net/article/126786.htm ModelForm组件如同它的名字一样就是把model和form结合起来,在有些场景可以起到意想不到的效果. 先来 ...
- GIS学习和开发的在线资源
1.OpenGIS Consortium标准,http://www.opengeospatial.org.著名的OGC标准是每个GIS开发者最后都不得不学习的,或深或浅. 2.SharpMap,Pro ...
- 20160418 while,switch,do..while的使用
9 一.While循环 示例:求100以内所有数的和 Int i=1;//初始条件 Int sum=0; While(i<=100)//循环条件 { Sum+=i;//循环体 i++;//状态改 ...
- 使用idea2016导出web项目war包
第一步配置Web Application:Exploded(已经配置的可以跳到第二步): 打开project structure(默认的快捷键是Ctrl+Alt+Shift+S),依次选择Artifa ...
- MSDN使用
比如我想查一下fopen这个函数怎么用,在索引里搜索一下fopen,很容易找到了. 但是如果我想横向扩展一下,查看一些与fopen相关的函数,应该怎么找呢? 很简单,点击定位: 你就能把fopen定位 ...
- 每天一个Linux命令(38)top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. (1)用法: 用法: top [参数] top是 ...
- 【leetcode刷提笔记】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- node拦截器设置
node的拦截器主要目的是用户登录的时候为用户存了一个session,用户登录后的其他操作都要经过拦截器,对比session的值,并把session的过期时间延长. 拦截器主要是在路由文件routes ...