工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1582|回复: 3

hibernate 一对一

[复制链接]
发表于 2006-9-5 18:44 | 显示全部楼层 |阅读模式
hibernate 一对一实践 author: hjack(好好的一篇文章,可惜现在只剩下一半)

以前看过,记得是以 many-to-one 实现一对一的。于是我自己去实现看看,结果是可行的。但是我觉得,如果只是单向的一对一,直接用 one-to-one就可以了。

我没有做实际项目,假想的例子为我们中国的一夫一妻制。两个类分别为:Husband Wife,这个算是典型的一对一关系吧?!(呵呵~)

数据厍DDL为:

  1.   create table `sampledb`.`husband`(
  2.         `id` bigint not null auto_increment,
  3.        `marry_id` bigint,
  4.        `name` varchar(50),
  5.         primary key (`id`)
  6.     );
  7.   create table `sampledb`.`wife`(
  8.         `id` bigint not null auto_increment,
  9.        `marry_id` bigint,
  10.        `name` varchar(50),
  11.         primary key (`id`)
  12.     );
复制代码

我的 mysql 数据厍里早有名为sampledb的数据厍,如果没有,要先用 create database sampledb 创建。数据表 husband 和 wife 的内容为编号、配偶编号和姓名。
两个类文件分别为:

  1. package powerwind;

  2. public class Wife implements java.io.Serializable {

  3.         // Fields

  4.         private Long id;

  5.         private Husband husband;

  6.         private String name;

  7.         // Constructors

  8.         /** default constructor */
  9.         public Wife() {
  10.         }

  11.         /** full constructor */
  12.         public Wife(Husband husband, String name) {
  13.                 this.husband = husband;
  14.                 this.name = name;
  15.         }

  16.         // Property accessors

  17.         public Long getId() {
  18.                 return this.id;
  19.         }

  20.         public void setId(Long id) {
  21.                 this.id = id;
  22.         }

  23.         public Husband getHusband() {
  24.                 return this.husband;
  25.         }

  26.         public void setHusband(Husband husband) {
  27.                 this.husband = husband;
  28.         }

  29.         public String getName() {
  30.                 return this.name;
  31.         }

  32.         public void setName(String name) {
  33.                 this.name = name;
  34.         }

  35. }
复制代码


  1. package powerwind;

  2. public class Husband implements java.io.Serializable {

  3.         // Fields

  4.         private Long id;

  5.         private Wife wife;

  6.         private String name;

  7.         // Constructors

  8.         /** default constructor */
  9.         public Husband() {
  10.         }

  11.         /** full constructor */
  12.         public Husband(Wife wife, String name) {
  13.                 this.wife = wife;
  14.                 this.name = name;
  15.         }

  16.         // Property accessors

  17.         public Long getId() {
  18.                 return this.id;
  19.         }

  20.         public void setId(Long id) {
  21.                 this.id = id;
  22.         }

  23.         public Wife getWife() {
  24.                 return this.wife;
  25.         }

  26.         public void setWife(Wife wife) {
  27.                 this.wife = wife;
  28.         }

  29.         public String getName() {
  30.                 return this.name;
  31.         }

  32.         public void setName(String name) {
  33.                 this.name = name;
  34.         }
  35. }
复制代码

两个类的配制文件几乎一样,下面只给出一个:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
        <class name="powerwind.Husband" table="husband"
                catalog="sampledb">
                <id name="id" type="java.lang.Long">
                        <column name="id" />
                        <generator class="native"></generator>
                </id>
                <many-to-one name="wife" class="powerwind.Wife"
                        fetch="select">
                        <column name="marry_id" />
                </many-to-one>
               
                 <property name="name" type="java.lang.String">
                        <column name="name" length="50" />
                </property>
        </class>
</hibernate-mapping>

这样竟然就行了,至于 hibernate.cfg.xml,和其它的一样,没有什么特别。再自行写个类测试下就可以了。我测试了几次,还没有发现错误,应该是可行的。

[ 本帖最后由 powerwind 于 2006-9-5 18:47 编辑 ]
发表于 2006-9-5 21:57 | 显示全部楼层
我那张贴从旧后院找回来了.
回复

使用道具 举报

 楼主| 发表于 2006-9-5 22:36 | 显示全部楼层
果然厉害,回来了耶!
觉得我没有在 many-to-one 添加 unique="true",设计欠妥,要在程序中控制唯一性。
还有,one-to-one 好像只适合共享主键的情况。
回复

使用道具 举报

 楼主| 发表于 2006-9-6 13:40 | 显示全部楼层
发现网上有文章介绍一对一关系的,没有用两个many-to-one的,而是其中一个用one-to-one
如: http://www.javaworld.com.tw/conf ... .action?pageId=1819
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 10:14

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表