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后,可以看到如下页面,右侧是各个版本 ...
随机推荐
- Oracle学习笔记—数据字典和常用命令(转载)
转载自: oracle常用数据字典和SQL语句总结 Oracle常用命令大全(很有用,做笔记) 一.Oracle数据字典 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的.比如一 ...
- Junit单元测试注入spring中的bean(转载)
转载自:http://blog.csdn.net/cy104204/article/details/51076678 一般对于有bean注入的类进行方法单元测试时,会发现bean对象并没有注入进来,对 ...
- Personal Ubuntu
@1:修改了打开主文件夹的快捷键 CTRL + ALT + R @2:增加了强制关闭程序的快捷键CTRL + ALT+ X,程序为~/lxw0109/ownAdmin/closeProgram.sh( ...
- django-admin 设计User外键,设计model
设置外键 class profile_user(AbstractBaseUser, PermissionsMixin): company = models.ForeignKey(Company, de ...
- Android开发问题:ActivityNotFoundException: Unable to find explicit activity class
http://blog.csdn.net/debuglog/article/details/7236013 原因:AndroidManifest.xml未添加对应Activity配置. 解决办法:在A ...
- 软件工作考核项(zcl)——
注意:这里没有对代码风格做要求,因为要代码走查! 考核项 考核标准 分数等级 需求规格说明书编写 主要用例图缺失 -1 主要软件界面设计图缺失 -1 主要功能清单项目缺失 -1 主要复 ...
- Android Integer.parseInt java.lang.NumberFormatException: Invalid int解决方法
解决方法: http获取的字符串minutes去空字符串处理minutes.replaceAll("\\D+","").replaceAll("\r& ...
- Kattis - register 【水】
题意 就是 有一堆容器,然后可以执行加的操作,每个容量是 2, 3, 5, 7, 11, 13, 17, 19 然后 有进位 比如第一个 容器,到2了,就会重置为0,然后 下一个容器+ 1, 但是要保 ...
- VM上安装苹果虚拟机
用了太久的Windows系统,看着Mac OS X的惊艳,相信很多朋友也和我一样,总想着能把玩一把Mac OS X系统吧?如果只是为了体验一下Mac OS X系统而购买一套Mac电脑,那是土豪做的事. ...
- 【Tech】SQL Server2008使用笔记
1.64位win7系统报错“未在本地计算机上注册 Microsoft.ACE.OLEDB.12.0 ”. 解决方法:从这个网址下载和安装一个驱动http://www.microsoft.com/zh- ...