`
zzc1684
  • 浏览: 1191467 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Hibernate关联配置(XML)

阅读更多

(many to one)

many-to-one关联是最常关联关系。

<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>

</class>

create table Person ( personId bigint not null primary key, addressId bigint not null )

create table Address ( addressId bigint not null primary key

8.2.2一(one to one

基于外键关联向一关联向多关联几乎是一的。唯一的不同就是向一关联中的外字段具有唯一性束。

<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>

</class>

create table Person ( personId bigint not null primary key, addressId bigint not null unique )

create table Address ( addressId bigint not null primary key )

基于主键关联向一关联通常使用一个特定的id生成器。(注意,在个例子中我关联的方向。)

<class name="Person">

    <id name="id" column="personId"><generator class="native"/></id>

</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 )

8.2.3多(one to many

基于外键关联向一关联是一很少的情况,并不推荐使用。

<class name="Person">

    <id name="id" column="personId"><generator class="native"/></id>

    <set name="addresses">

        <key column="personId" not-null="true"/>

        <one-to-many 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 Address ( addressId bigint not null primary key, personId bigint not null )

们认为对这种关联关系最好使用接表。

8.3. 使用接表的关联Unidirectional associations with join tables

8.3.1(one to many)

基于接表的向一关联 应该优先被采用。注意,通指定unique="true",我可以把多性从多多改变为多。

<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" unique="true" 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 not null, addressId bigint not null primary key )

create table Address ( addressId bigint not null primary key )

8.3.2一(many 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"/></join>

</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 primary key, addressId bigint not null )

create table Address ( addressId bigint not null primary key )

8.3.3一(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>

create table Person ( personId bigint not null primary key )

create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique )

create table Address ( addressId bigint not null primary key )

8.3.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 )

8.4. 双向关联Bidirectional associations

8.4.1多(one to many) / 一(many to one

双向多关联 是最常关联关系。(也是准的父/关联关系。)

<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 )

8.4.2一(one to one

基于外键关联的双向一关联也很常

<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 )

基于主键关联的一关联需要使用特定的id生成器。

<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 )

8.5. 使用接表的双向关联Bidirectional associations with join tables

8.5.1多(one to many /一( many to one

基于接表的双向一关联。注意inverse="true"可以出关联的任意一端,即collection端或者join端。

<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" unique="true" class="Address"/></set>

</class>

<class name="Address">

    <id name="id" column="addressId"><generator class="native"/></id>

    <join table="PersonAddress" inverse="true" optional="true">

        <key column="addressId"/>

        <many-to-one name="person" column="personId" not-null="true"/></join>

</class>

create table Person ( personId bigint not null primary key )

create table PersonAddress ( personId bigint not null, addressId bigint not null primary key )

create table Address ( addressId bigint not null primary key )

8.5.2一(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>

    <join table="PersonAddress" optional="true" inverse="true">

        <key column="addressId" unique="true"/>

        <many-to-one name="address" column="personId" not-null="true" unique="true"/>

    </join>

</class>

create table Person ( personId bigint not null primary key )

create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique )

create table Address ( addressId bigint not null primary key )

8.5.3多(many to many

最后, 双向多关联.

<class name="Person">

    <id name="id" column="personId"><generator class="native"/></id>

    <set name="addresses">

        <key column="personId"/>

        <many-to-many column="addressId" class="Address"/></set>

</class>

<class name="Address">

    <id name="id" column="addressId"><generator class="native"/></id>

    <set name="people" inverse="true">

        <key column="addressId"/>

        <many-to-many column="personId" class="Person"/></set>

</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 )

 

分享到:
评论

相关推荐

    Hibernate1对多1对1多对多关联映射例子源码含xml配置

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,这个是xml配置的例子

    Hibernate各种数据库关联annotatian和XML的配置集锦

    关于hibernate3.0各种数据库关联的小列子,里面的内容分的很仔细很有条理。对于初学者很有帮助。

    Hibernate双向一对一关联映射(注解版)

    Hibernate双向一对一关联映射(注解版)

    hibernate的xml配置关系例子

    由于上传最大是60m,这里只是上传关联的配置xml和实体类,环境搭建自己找度娘解决

    Hibernate1对多1对1多对多关联映射例子源码含注解配置

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式以后,发现使用...

    hibernate 体系结构与配置 参考文档(html)

    Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 1.3.5...

    hibernate中文帮助及语法配置

    详细的介绍hibernate的所有配置映射、事务、xml的配置、多态查询、关联等核心中文帮助,提供源代码参考

    springmvc+hibernate 日志管理工具

    数据日志:设计中提供数据日志注入接口,管理数据日志注入,业务系统可过基于hibernate Listener 拦截数据表操作,注入数据日志(此为业务系统实现,设计中提供数据日志注入接口),提供 日志管理:通过统一的...

    Hibernate 课件_配置文件详解

    这是第二篇, 内容如下:Hibernate配置文件;jdbc.fetch_size;jdbc.batch_size; POJO 类和数据库的映射文件*.hbm.xml;主键生成策略generator;映射集合属性;延迟加载策略;映射组件属性;关联关系映射:双向 1-N;继承...

    Hibernate+中文文档

    1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 ...

    hibernate3.2中文文档(chm格式)

    1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 ...

    HibernateAPI中文版.chm

    1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 ...

    Hibernate使用技巧汇总

    两种配置文件: A.hibernate.cfg.xml 和 B.hibernate.properties A中可含映射文件的配置,而B中hard codes加映射文件。 A。Configuration config=new Configuration().config(); B. ...

    hibernate+中文api

    前言 1. 翻译说明 2. 版权声明 1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用...3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory

    hibernate 3中的缓存小结

    2) 在hibernate.cfg.xml文件中加入EhCache缓存插件的提供类。 &lt;!--配置缓存插件 --&gt; &lt;property name="hibernate.cache.provider_class"&gt; org.hibernate.cache.EhCacheProvider 3) 挎贝ehcache.xml文件到类...

    Hibernate Annotations 中文文档

    1.2. 系统配置 2. 实体Bean 2.1. 简介 2.2. 用EJB3注解进行映射 2.2.1. 声明实体bean 2.2.1.1. 定义表(Table) 2.2.1.2. 乐观锁定版本控制 2.2.2. 映射简单属性 2.2.2.1. 声明基本的属性映射 2.2.2.2. 声明列...

    Hibernate中文详细学习文档

    1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 ...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射文件  3.4.1 映射文件的文档类型定义(DTD)  3.4.2 把Customer持久化类映射到CUSTOMERS表  3.5 通过...

    Hibernate 中文 html 帮助文档

    1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 ...

    Hibernate实战(第2版 中文高清版)

     2.1.3 Hibernate配置和启动   2.1.4 运行和测试应用程序   2.2 启动Java Persistence项目   2.2.1 使用Hibernate Annotations   2.2.2 使用Hibernate EntityManager   2.2.3 引入EJB组件   2.2.4 切换...

Global site tag (gtag.js) - Google Analytics