Hibernate之关系映射的两种方式(xml映射、annotation注解映射)
2016-12-14 20:11
639 查看
本文列出了两种关系映射包括一对一和一对多(多对一),并且列出了两种方式(xml映射和annotation注解映射)分别对一对一和一对多的代码,关键地方标有注释解释。可供初学者参考。
欢迎初学者加入群:583763961一起交流学习,也欢迎大牛加群指导。共同学习,共同进步。
一.xml文件映射
1.在Student类中加入一个对方类型的属性,即登录信息类的实例。
2.配置student.hbm.xml,基本属性配置方法不变,StudentLogin的配置如下。
3.在hibernate.cfg.xml注册student.hbm.xml文件
二.annotation注解映射
1.在hibernate.cfg.xml注册StudentLogin类
2.在Student中添加注解
一.xml文件映射
2.配置student.hbm.xml,基本属性配置方法不变,StudentStatus的配置如下。
2.配置studentStatus.hbm.xml,基本属性配置方法不变,Students的配置如下。
二、annotation注解映射
2.在Student类中添加注解
2.在StudentStatus类中添加注解
欢迎初学者加入群:583763961一起交流学习,也欢迎大牛加群指导。共同学习,共同进步。
主键关系映射代码之一对一##
一个学生对应一条登录信息(用户名、密码),一条登录信息对于一个学生一.xml文件映射
1.在Student类中加入一个对方类型的属性,即登录信息类的实例。
package com.ffy.studentmanage; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; public class Student { private int id; private int sid; private String firstName; private String lastName; private String gender; private String phone; private String email; private String dateOfBirth; private double height=160; private int studentStatusID; //在student表中加入StudentLogin属性 private StudentLogin studentLogin; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastname() { return lastName; } public void setLastname(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(String dateOfBirth) { this.dateOfBirth = dateOfBirth; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getStudentStatusID() { return studentStatusID; } public void setStudentStatusID(int studentStatusID) { this.studentStatusID = studentStatusID; } public StudentLogin getStudentLogin() { return studentLogin; } public void setStudentLogin(StudentLogin studentLogin) { this.studentLogin = studentLogin; } public String toString(){ return this.id+":"+this.lastName+this.firstName+this.gender+":"+this.phone+":"+this.email+";username"+studentLogin.getUsername()+";password:"+studentLogin.getPassword(); } }
2.配置student.hbm.xml,基本属性配置方法不变,StudentLogin的配置如下。
<?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.ffy.studentmanage.Student" table="student"> <id name="id" column="id"> <generator class="assigned"></generator> </id> <property name="sid" column="sid"></property> <property name="firstName" column="firstname"></property> <property name="lastName" column="lastname"></property> <property name="gender" column="gender"></property> <property name="phone" column="phone"></property> <property name="email" column="email"></property> <property name="dateOfBirth" column="date_of_birth"></property> <property name="studentStatusID" column="student_status_id"></property> <property name="height" column="height"></property> //对方的属性的映射,name属性为,Student的studentLogin属性,class为对方类,意味将studentLogin属性与studentLogin类建立一对一的映射关系 <one-to-one name="studentLogin" class="com.ffy.studentmanage.StudentLogin" cascade="all"></one-to-one> </class> </hibernate-mapping>
3.在hibernate.cfg.xml注册student.hbm.xml文件
<mapping resource="StudentLogin.hbm.xml"/>
二.annotation注解映射
1.在hibernate.cfg.xml注册StudentLogin类
<mapping class="com.ffy.studentmanage.StudentLogin"/>
2.在Student中添加注解
package com.ffy.studentmanage; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name="student") public class Student { @Id @Column(name="id") private int id; @Column(name="sid") private int sid; @Column(name="firstname") private String firstName; @Column(name="lastname") private String lastName; @Column(name="gender") private String gender; @Column(name="phone") private String phone; @Column(name="email") private String email; @Column(name="date_of_birth") private String dateOfBirth; @Column(name="height") private double height=160; @Column(name="student_status_id") private int studentStatusID; //在student表中加入StudentLogin属性,并标注为一对一属性 @OneToOne @PrimaryKeyJoinColumn private StudentLogin studentLogin; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastname() { return lastName; } public void setLastname(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(String dateOfBirth) { this.dateOfBirth = dateOfBirth; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getStudentStatusID() { return studentStatusID; } public void setStudentStatusID(int studentStatusID) { this.studentStatusID = studentStatusID; } public StudentLogin getStudentLogin() { return studentLogin; } public void setStudentLogin(StudentLogin studentLogin) { this.studentLogin = studentLogin; } public String toString(){ return this.id+":"+this.lastName+this.firstName+this.gender+":"+this.phone+":"+this.email+";username"+studentLogin.getUsername()+";password:"+studentLogin.getPassword(); } }
主键关系映射代码之多对一(一对多)##
一个学生对应一条状态,一条状态对应多个学生一.xml文件映射
多方代码#
1.在Student类(多的那方)中加入一个对方类型的属性,即登录信息类的实例。@Table(name="student") public class Student { private int id; private int sid; private String firstName; private String lastName; private String gender; private String phone; private String email; private String dateOfBirth; private double height=160; //多的一方,加入对方的一个类的属性。一个学生对于一个状态。 private StudentStatus status; private StudentLogin studentLogin; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastname() { return lastName; } public void setLastname(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(String dateOfBirth) { this.dateOfBirth = dateOfBirth; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public StudentStatus getStatus() { return status; } public void setStatus(StudentStatus status) { this.status = status; } public StudentLogin getStudentLogin() { return studentLogin; } public void setStudentLogin(StudentLogin studentLogin) { this.studentLogin = studentLogin; } public String toString(){ return this.id+":"+this.lastName+this.firstName+this.gender+":"+this.phone+":"+this.email+";username"+studentLogin.getUsername()+";password:"+studentLogin.getPassword(); } }
2.配置student.hbm.xml,基本属性配置方法不变,StudentStatus的配置如下。
<class name="com.ffy.studentmanage.Student" table="student"> <id name="id" column="id"> <generator class="assigned"></generator> </id> <property name="sid" column="sid"></property> <property name="firstName" column="firstname"></property> <property name="lastName" column="lastname"></property> <property name="gender" column="gender"></property> <property name="phone" column="phone"></property> <property name="email" column="email"></property> <property name="dateOfBirth" column="date_of_birth"></property> <property name="studentStatusID" column="student_status_id"></property> <property name="height" column="height"></property> <one-to-one name="studentLogin" class="com.ffy.studentmanage.StudentLogin" cascade="all"></one-to-one> //表明多个学生可以对应一条状态,name对应Student类中的属性status,id为student_status表中的id字段(即student表中有一个外键student_status_id,对应student_status表的id字段,class表示一的一方为StudentStatus类) <many-to-one name="status" column="id" class="com.ffy.studentmanage.StudentStatus"></many-to-one> </class>
一方代码
1.在StudentStatus类(一的那方)中加入一个对方(多方)类型的属性,即学生信息类的实例。public class StudentStatus { private int id; private String code; private String name; private String description; //加入一个集合,泛型为Student,用于存放该状态对应的学生,因为一条状态对呀多个学生,所以需要用集合表示 private Set<Student> students=new HashSet<>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } public String toString(){ return "id:"+this.id+";name"+this.name+";code:"+this.code; } }
2.配置studentStatus.hbm.xml,基本属性配置方法不变,Students的配置如下。
<hibernate-mapping> <class name="com.ffy.studentmanage.StudentStatus" table="student_status"> <id name="id" column="id"> <generator class="assigned"></generator> </id> <property name="code" column="code"></property> <property name="name" column="name"></property> <property name="description" column="description"></property> //集合存放该状态对应的多个学生,name为属性名(即集合名称),column表示相关联的外键, <set name="students" cascade="all" inverse="true"> <key column="student_status_id"></key> //表示一个状态对应多个学生,calss表示对应的多的那方的类(即与之相对应的类) <one-to-many class="com.ffy.studentmanage.Student"/> </set> </class> </hibernate-mapping>
二、annotation注解映射
多的那方
1.在hibernate.cfg.xml注册Student类<mapping class="com.ffy.studentmanage.Student"/>
2.在Student类中添加注解
@Entity @Table(name="student") public class Student { @Id @Column(name="id") private int id; @Column(name="sid") private int sid; @Column(name="firstname") private String firstName; @Column(name="lastname") private String lastName; @Column(name="gender") private String gender; @Column(name="phone") private String phone; @Column(name="email") private String email; @Column(name="date_of_birth") private String dateOfBirth; @Column(name="height") private double height=160; /* * 多的一方,加入对方的一个类的属性。一个学生对于一个状态。 * name="student_status_id"表明一的那方为外键student_status_id锁对应的表 */ @ManyToOne @JoinColumn(name="student_status_id") private StudentStatus status; @OneToOne @PrimaryKeyJoinColumn private StudentLogin studentLogin; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastname() { return lastName; } public void setLastname(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(String dateOfBirth) { this.dateOfBirth = dateOfBirth; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public StudentStatus getStatus() { return status; } public void setStatus(StudentStatus status) { this.status = status; } public StudentLogin getStudentLogin() { return studentLogin; } public void setStudentLogin(StudentLogin studentLogin) { this.studentLogin = studentLogin; } public String toString(){ return this.id+":"+this.lastName+this.firstName+this.gender+":"+this.phone+":"+this.email+";username"+studentLogin.getUsername()+";password:"+studentLogin.getPassword(); } }
一的那方#
1.在hibernate.cfg.xml注册StudentStatus类<mapping class="com.ffy.studentmanage.StudentStatus"/>
2.在StudentStatus类中添加注解
package com.ffy.studentmanage; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; @Table(name="student_status") @Entity public class StudentStatus { @Id() @Column(name="id") private int id; @Column(name="code") private String code; @Column(name="name") private String name; @Column(name="description") private String description; /* * 加入一个集合,泛型为Student,用于存放该状态对应的学生,因为一条状态对呀多个学生,所以需要用集合表示 * name="id":student_status表的主键为Student表的外键,这样子就能找到Student(多的那方) * 的student_status_id为student_status表的id的字段,两者对应 */ @OneToMany @JoinColumn(name="id") private Set<Student> students=new HashSet<>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } public String toString(){ return "id:"+this.id+";name"+this.name+";code:"+this.code; } }
相关文章推荐
- 4、Hibernate 实体关系映射(一、单边一对多关系 注解、xml两种方式)
- 4、Hibernate 实体关系映射(二、单边多对一关系 注解、xml两种方式)
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- Hibernate中,One2Many和Many2One的双向映射关系的基于注解Annotation实现方式
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- 注解方式实现 Hibernate 的双向一对多关系的映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate之Annotation(注解的方式,非映射)
- Hibernate关系映射(七)多对多双向关联@ManyToMany Annotation方式
- Hibernate基于注解方式配置来实现实体和数据库之间存在某种映射关系
- Hibernate关系映射(四)一对多单向关联@OneToMany Annotation方式
- 前几天用AnnotationSessionFactoryBean 注解方式代替*.hbm.xml时扫描映射实休文件后报错
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- hibernate注解方式来处理映射关系