hibernate 关系映射 one-to-one主要有三种实现方式
1.通过外键方式实现
以学生和电脑为例(Student-Computer)
建表sql语句:
- 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;
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:
- 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;
- }
- }
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:
- 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;
- }
- }
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:
- <?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>
<?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:
- <?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.Computer" table="computer" catalog="onetoone">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native"