一、关系映射简介
在数据库中,表与表的关系,仅有外键。但使用hibernate后,为面向对象的编程,对象与对象的关系多样化;如 一对一,一对多,多对多,并具有单向和双向之分。
开始练习前,复制上一次项目,并更名为
二、一对一单向外键关联
例如:一个老师只教一个学生,一个学生只能被一个老师教
关系图如:
修改teahcer.java和student.java类(为了简单起见吧Student也更改为使用Annotation的形式)
package com.model; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="teacher")
public class Teacher {
private int id;
private String name;
private int age;
private String title; @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
接着删除数据库里的teahcer和student表,在hibernate.cfg.xml的配置文件里,有一条
<property name="hbm2ddl.auto">update</property>
参数为update时,会在第一次运行时检查是否有表结构,没有则自动生成。我让hibernate自动生成表,看看一对一单向关联的表结构是怎样的。
运行测试。
public class Main {
public static void main(String[] args) {
//通过util类创建sessionFactory
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
sessionFactory.close();
}
}
三、结果展示
结果为在student表中出现了一个外键,依赖teacher的主键id,而teacher中并没有外键;
四、一对一双向外键关联
忽略关系图;
修改teacher.java,添加属性
private Student student;
@OneToOne
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
删除表,并运行测试;
结果展示:
两张表都生成了一个外键,并依赖另一张表的主键id;
为了不创建两个外键(效果重复。。。),在其中一个@OneToOne上设置(mappedBy);
例如在student.java上设置;
@OneToOne(mappedBy="student")
mappedBy="student" 中student为teacher里的student属性;
测试运行后,结果只有teacher表中有外键,student表里没有;
PS:建议只要有双向关联,一定要设置mappedBy;
PSS:主键关联忽略,使用时现查现用;
五、组件映射
将一个普通的类作为一个实体类的一部分,里面的属性作为实体类的表里的字段,叫做组件映射;
先将teacher.java类里的Annotation删除,并删除id属性,为了避免字段名重复错误,把teacher类里的name和age属性改为teacherName和teacherAge;
package com.model; public class Teacher {
private String teacherName;
private int teacherAge;
private String title; public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public int getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(int teacherAge) {
this.teacherAge = teacherAge;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
修改student.java;
package com.model; import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table; @Entity
@Table(name="student")
public class Student {
private int id;
private String name;
private int age;
private Teacher teacher; @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Embedded
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
} }
运行测试;
没有teacher这张表,student表里多了三个字段;
PS:一对一单向外键关联与一对一双向外键关联在数据库的表的格式是一样的,区别在于
java程序中. 双向外键关联可通过Hibernate在两个类间互相调用彼此,而单向外键关联只能单方向调用。
----------------------------------------------------------------------------------------------------
此文为个人学习记录,如有参考转载,请注明出处 黑白熊的博客 http://www.cnblogs.com/xiong233/