hibernate之 8.one2one单向

表结构:




类图:



CRUD:

配置:

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 关系,且有User这一方来维护关联字段


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 ;
	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;
	}
	
}


add:

@Test
	public void addTest(){
		Student student=new Student();
		student.setStudentName("赵虎");
		student.setAge(35);
		
		User user=new User();
		user.setUserName("zhf");
		user.setPassword("111111");
		user.setStudent(student);
		
		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(){
		Student student=new Student();
		student.setStudentId(63);
		User user=new User();
		user.setUserId(1);
		user.setStudent(student);
		user.setUserName("yy");
		
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.update(user);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}

发出的SQL

Hibernate: update t_user set user_name=?, password=?, student_id=? where user_id=?

由User这方来维护关联字段


load

@Test
	public void loadTest(){
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			User user=(User)session.load(User.class, 1);
			System.out.println("用户名:"+user.getUserName());
			System.out.println("学员姓名:"+user.getStudent().getStudentName());
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}

发出的sql:

Hibernate: select user0_.user_id as user1_2_0_, user0_.user_name as user2_2_0_, user0_.password as password2_0_, user0_.student_id as student4_2_0_ from t_user user0_ where user0_.user_id=?
用户名:yy
Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?
学员姓名:李九


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。