at sometime we usually need to create two tables that one table relate another.Such as a husband only

have a wife. So how can I design relationship like this.In programming, The type of this relationship

named “unidirectional one-to-one association”.

How to implement this relationship with hibernate?

example for husband and wife.

When create object husband and wife, you can relate two object like this:

The Hasband object:

   1:  @Entity
   2:  public class Husband {
   3:      private int id;
   4:      private String name;
   5:      private Wife wife;
   6:      @Id
   7:      @GeneratedValue
   8:      public int getId() {
   9:          return id;
  10:      }
  11:      public void setId(int id) {
  12:          this.id = id;
  13:      }
  14:      public String getName() {
  15:          return name;
  16:      }
  17:      public void setName(String name) {
  18:          this.name = name;
  19:      }
  20:      @OneToOne
  21:      @JoinColumn(name="wifeId")
  22:      public Wife getWife() {
  23:          return wife;
  24:      }
  25:      public void setWife(Wife wife) {
  26:          this.wife = wife;
  27:      }
  28:  }

The Wife object:

   1:  @Entity
   2:  public class Wife {
   3:      private int id;
   4:      private String name;
   5:      @Id
   6:      @GeneratedValue
   7:      public int getId() {
   8:          return id;
   9:      }
  10:      public void setId(int id) {
  11:          this.id = id;
  12:      }
  13:      public String getName() {
  14:          return name;
  15:      }
  16:      public void setName(String name) {
  17:          this.name = name;
  18:      }
  19:      
  20:  }

We use annotation configuration hibernate by default. So you saw the annotition like @Entity in code.

Wife and Husband both are used @Entity.It means that object will mapping to database.

The getId() method in Wife and Husband both are used @Id and @GeneratedValue

The getWife() method in Husband object has annotation like this:

@OneToOne
      @JoinColumn(name="wifeId")

Then, the column which is named wifeId wille be created.

So, Use annotation is so simple.  that;s all.

Do from that we can get create table SQL like this:

   1:      create table Husband (
   2:          id integer not null auto_increment,
   3:          name varchar(255),
   4:          wifeId integer,
   5:          primary key (id)
   6:      )
   7:  2014-1-11 14:46:41 org.hibernate.tool.hbm2ddl.SchemaExport perform
   8:  
   9:   
  10:      create table Wife (
  11:          id integer not null auto_increment,
  12:          name varchar(255),
  13:          primary key (id)
  14:      )
  15:   
  16:      alter table Husband 
  17:          add constraint FK_kruq9jfxa0jrc2od8dbh09mia 
  18:          foreign key (wifeId) 
  19:          references Wife (id)

Finally , Talk about xml configure. When use xml to configure hibernate more trouble than use annotations:

You need write like this in xml file:

<many-to-one name="wife" column="wifId" unique="true" ></many-to-one>

Hibernate -- A unidirectional one-to-one association on a foreign key的更多相关文章

  1. hibernate部分源码解析and解决工作上关于hibernate的一个问题例子(包含oracle中新建表为何列名全转为大写且通过hibernate取数时如何不用再次遍历将列名(key)值转为小写)

    最近在研究系统启动时将数据加载到内存非常耗时,想着是否有办法优化!经过日志打印测试发现查询时间(查询时间:将数据库数据查询到系统中并转为List<Map>或List<*.Class& ...

  2. Hibernate级联删除时:Cannot delete or update a parent row: a foreign key constraint fails异常

    在删除主表数据时,报了一个异常 Cannot delete or update a parent row: a foreign key constraint fails 原因是主表中还包含字表的数据, ...

  3. hibernate 中文文档

    转载:http://blog.csdn.net/kevon_sun/article/details/42850387 Hibernate Annotations 参考文档 3.2.0 CR1 目录 前 ...

  4. eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法

    当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...

  5. hibernate学习(一)配置,导包

    框架的作用 学过javaWeb基础的已经对web层 jsp  servlet   ,service  层  ,dao层的jdbc .DBUtils 有了很深的了解 并编写代码实现某种功能 为了提高开发 ...

  6. hibernate 多对多(many-to-many)

    多对多(many-to-many):在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型:hibernate会为我们创建中间关联表,转换成两个一对多. 1. E- ...

  7. [Hibernate系列—] 3. 映射文件和使用SchemaExport制作自己主动Schema

    自己定义映射文件 这里的映射文件指的是相应到数据库表的xml 的定义文件. 相应的每一个数据库表栏位, 能够定义的属性有: 属性名 类型 Description length number 栏位的长度 ...

  8. Hibernate的dtd文件和properties文件

    hibernate-configuration-3.0.dtd <!-- Hibernate file-based configuration document. <!DOCTYPE hi ...

  9. Struts2+Spring+Hibernate框架整合总结详细教程

    一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...

随机推荐

  1. .NET程序优化

    一.数据库操作 1. 用完马上关闭数据库连接 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证, 比较耗费服务器资 源.ASP.NET 中提供了连 ...

  2. Bootstrap Table Examples

    The examples of bootstrap table http://bootstrap-table.wenzhixin.net.cn/examples/ http://www.jq22.co ...

  3. windows下C++环境的配置

    方法一--VS: 使用windows开发神器visio studio.这种方法比较简单,直接下载一个最新的vs安装就行.不单单是C++,C.C#.VB等都可以开发. 方法二--只安装C++编译器: 最 ...

  4. 1.jenkins持续集成-jenkins安装

    1.为什们要使用jenkins Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目; 2.监控外部调用执行的工作. 2.安装jen ...

  5. HTTP 协议缓存

    http 缓存分为客户端缓存和服务端缓存 1. 客户端缓存 客户端缓存指的是浏览器缓存, 浏览器缓存是最快的缓存, 因为它直接从本地获取(但有可能需要发送一个请求), 它的优势是可以减少网络流量, 加 ...

  6. json 数据 添加 删除 排序

    js数据格式和json数据格式,各有各的用处,就个人而言,json更好用一点,js自身的数组和对像限制比较多. 以js的数组举例: var a = ['1']; a[5] = 52; a.length ...

  7. 配置文件keepalived.conf详解

    keepalived.conf       一个功能比较完整的keepalived 的配置文件,其配置文件keepalived.conf 可以包含三个文本块:全局定义块.VRRP 实例定义块及虚拟服务 ...

  8. jQuery动画特效笔记

    show().hide().fadeIn().fadeOut().slideDown.slideUp.slideToggle()都接受可选的时长和回调参数(选项对象参数). toggle(durati ...

  9. jsp中文件上床的enctype="multipart/form-data"用法

    jsp表单中必须要有enctype="multipart/form-data"才能文件上传的意思,是设置表单的MIME编码.默认情况,这个编码格式是application/x-ww ...

  10. ueditor问题简记录

    一.百度ueditor下载地址:http://ueditor.baidu.com/website/download.html. uBuilder下载,个人选了一些自用的,.net的,但是很奇怪下载响应 ...