首页 > 代码库 > hibernate 之 9.one2one双向
hibernate 之 9.one2one双向
在培训系统中,我们经常会为每一个学员,分配一个所属登录帐号,对应的表结构设计如下:
图1:培训系统表结构
t_student:
学员信息表,存储学员的基本信息,如:姓名、年龄、身份证号、地址等等
t_user:
用户表,储系统用户信息,如:登录帐号、密码等
从图1中,知道 它通过student_id来关联 t_studnet
面向对象类关系
CRUD:
配置:
User类
package com.demo.model; /**用户信息 * @author wobendiankun *2014-10-29 下午11:05:26 */ public class User { /** *用户Id */ private int userId; /** * 用户名 */ private String userName; /** * 密码 */ private String password; private Student student ; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
Student类package com.demo.model; import java.io.UnsupportedEncodingException; import java.util.Set; /**学生信息 * @author wobendiankun *2014-10-19 下午08:54:29 */ public class Student { private int studentId ; private String studentName ; private int age; private Set<Certificate> certificates ; private User user; public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { String str=""; if(studentName!=null){ try { str=new String(studentName.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return "Student [studentId=" + studentId + ", studentName=" + str + ", age=" + age + "]"; } public Set<Certificate> getCertificates() { return certificates; } public void setCertificates(Set<Certificate> certificates) { this.certificates = certificates; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
User.hbm.xml<?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="com.demo.model.User" table="t_user"> <id name="userId" column="user_id"> <generator class="sequence"> <param name="sequence">SEQ_T_USER</param> </generator> </id> <property name="userName" column="user_name" /> <property name="password" /> <many-to-one name="student" column="student_id" unique="true"> </many-to-one> </class> </hibernate-mapping>
<many-to-one name="student" column="student_id" unique="true"> ,通过 unique="true" 来说明1对1关系,并由这方来维护关联字段
Student.hbm.xml
<?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="com.demo.model.Student" table="t_student"> <id name="studentId" column="student_id"> <generator class="sequence"> <param name="sequence">SEQ_T_STUDENT</param> </generator> </id> <property name="studentName" column="student_name" /> <property name="age" /> <set name="certificates" lazy="extra" inverse="true"> <key column="student_id"></key> <one-to-many class="com.demo.model.Certificate"/> </set> <one-to-one name="user" property-ref="student"></one-to-one> </class> </hibernate-mapping>
<one-to-one name="user" property-ref="student"></one-to-one> ,
name为Student类的成员变量user,
property-ref为User类的成员变量student
add:
@Test public void addTest(){ Student student=new Student(); student.setStudentName("大宝"); student.setAge(45); User user=new User(); user.setUserName("db"); user.setPassword("111111"); //设置双向关系 user.setStudent(student); student.setUser(user); Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); session.save(student); session.save(user); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } }发出的sql:
Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?) Hibernate: insert into t_user (user_name, password, student_id, user_id) values (?, ?, ?, ?)
update@Test public void updateTest(){ User user=new User(); user.setUserId(21); user.setUserName("dddd"); Student student=new Student(); student.setStudentId(102); student.setStudentName("二宝"); //设置关联关系 student.setUser(user); Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); session.update(student); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } }
发出的sqlHibernate: update t_student set student_name=?, age=? where student_id=?
Student这方不负责维护关联字段,不会发送更新User 的语句
hibernate 之 9.one2one双向
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。