Hibernate one to one详解

hibernate 关系映射 one-to-one主要有三种实现方式
1.通过外键方式实现
以学生和电脑为例(Student-Computer)
建表sql语句:

Java代码 复制代码
  1. CREATE DATABASE `onetoone`   
  2. CHARACTER SET 'utf8';   
  3.   
  4. USE `onetoone`;   
  5.   
  6. DROP TABLE IF EXISTS `student`;   
  7. CREATE TABLE `student` (   
  8.   `id` int(11) NOT NULL auto_increment,   
  9.   `name` varchar(255) NOT NULL,   
  10.   PRIMARY KEY  (`id`)   
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;   
  12.   
  13. DROP TABLE IF EXISTS `computer`;   
  14. CREATE TABLE `computer` (   
  15.   `id` int(11) NOT NULL auto_increment,   
  16.   `name` varchar(255) NOT NULL,   
  17.   `student_id` int(11) ,   
  18.   foreign key (`student_id`) references student(`id`),   
  19.   PRIMARY KEY  (`id`)   
  20. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
CREATE DATABASE `onetoone`
CHARACTER SET 'utf8';

USE `onetoone`;

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `computer`;
CREATE TABLE `computer` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `student_id` int(11) ,
  foreign key (`student_id`) references student(`id`),
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



Student.java:

Java代码 复制代码
  1. package com.domain;   
  2.   
  3.   
  4. public class Student implements java.io.Serializable {   
  5.   
  6.     private Integer id;   
  7.   
  8.     private String name;   
  9.   
  10.     private Computer computer;   
  11.   
  12.     public Student() {   
  13.     }   
  14.   
  15.     public Student(String name) {   
  16.         this.name = name;   
  17.     }   
  18.   
  19.     public Integer getId() {   
  20.         return this.id;   
  21.     }   
  22.   
  23.     public void setId(Integer id) {   
  24.         this.id = id;   
  25.     }   
  26.   
  27.     public String getName() {   
  28.         return this.name;   
  29.     }   
  30.   
  31.     public void setName(String name) {   
  32.         this.name = name;   
  33.     }   
  34.   
  35.     public Computer getComputer() {   
  36.         return computer;   
  37.     }   
  38.   
  39.     public void setComputer(Computer computer) {   
  40.         this.computer = computer;   
  41.     }   
  42.   
  43.        
  44. }  
package com.domain;


public class Student implements java.io.Serializable {

	private Integer id;

	private String name;

	private Computer computer;

	public Student() {
	}

	public Student(String name) {
		this.name = name;
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

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

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

	public Computer getComputer() {
		return computer;
	}

	public void setComputer(Computer computer) {
		this.computer = computer;
	}

	
}


Computer.java:

Java代码 复制代码
  1. package com.domain;   
  2.   
  3. public class Computer implements java.io.Serializable {   
  4.   
  5.     private Integer id;   
  6.   
  7.     private Student student;   
  8.   
  9.     private String name;   
  10.   
  11.   
  12.     public Computer() {   
  13.     }   
  14.   
  15.   
  16.     public Computer(String name) {   
  17.         this.name = name;   
  18.     }   
  19.   
  20.   
  21.     public Computer(Student student, String name) {   
  22.         this.student = student;   
  23.         this.name = name;   
  24.     }   
  25.   
  26.     public Integer getId() {   
  27.         return this.id;   
  28.     }   
  29.   
  30.     public void setId(Integer id) {   
  31.         this.id = id;   
  32.     }   
  33.   
  34.     public Student getStudent() {   
  35.         return this.student;   
  36.     }   
  37.   
  38.     public void setStudent(Student student) {   
  39.         this.student = student;   
  40.     }   
  41.   
  42.     public String getName() {   
  43.         return this.name;   
  44.     }   
  45.   
  46.     public void setName(String name) {   
  47.         this.name = name;   
  48.     }   
  49.   
  50. }  
package com.domain;

public class Computer implements java.io.Serializable {

	private Integer id;

	private Student student;

	private String name;


	public Computer() {
	}


	public Computer(String name) {
		this.name = name;
	}


	public Computer(Student student, String name) {
		this.student = student;
		this.name = name;
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Student getStudent() {
		return this.student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

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

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

}


Student.hbm.xml:

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  4.   
  5. <hibernate-mapping>   
  6.     <class name="com.domain.Student" table="student" catalog="onetoone">   
  7.         <id name="id" type="java.lang.Integer">   
  8.             <column name="id" />   
  9.             <generator class="native" />   
  10.         </id>   
  11.         <property name="name" type="java.lang.String">   
  12.             <column name="name" not-null="true" />   
  13.         </property>   
  14.         <!-- class可以不写,因为根据name的值computer(属性),会通过反射自动找到属于哪个类的 -->   
  15.         <one-to-one cascade="delete,save-update" name="computer" class="com.domain.Computer" property-ref="student"></one-to-one>   
  16.     </class>   
  17. </hibernate-mapping>  
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.domain.Student" table="student" catalog="onetoone">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" not-null="true" />
        </property>
        <!-- class可以不写,因为根据name的值computer(属性),会通过反射自动找到属于哪个类的 -->
        <one-to-one cascade="delete,save-update" name="computer" class="com.domain.Computer" property-ref="student"></one-to-one>
    </class>
</hibernate-mapping>


Computer.hbm.xml:

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  4.   
  5. <hibernate-mapping>   
  6.     <class name="com.domain.Computer" table="computer" catalog="onetoone">   
  7.         <id name="id" type="java.lang.Integer">   
  8.             <column name="id" />   
  9.             <generator class="native"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值