hibernate笔记(三) 一对一 (主键关联)

Room.java

package com.goby.Hibernate.bean;

public class Room {
	
	private int id;
	
	private String address;
	
	private User users;

	public User getUsers() {
		return users;
	}

	public void setUsers(User users) {
		this.users = users;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
}

  

 

User.java

package com.goby.Hibernate.bean;

public class User {
	
	private int id;
	
	private String name;
	
	private Room room;

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

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

  

 

Room.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 
	package="com.goby.Hibernate.bean">

	<class name="Room" table="t_room">
		
		<id name="id">
			<generator class="foreign">
				<param name="property">users</param>
			</generator>
		</id>
		
		<property name="address"></property>
		<one-to-one name="users" constrained="true"/>
	</class>
	
</hibernate-mapping>

 

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 package="com.goby.Hibernate.bean">

	<class name="User" table="t_user">

		<id name="id">
			<generator class="native"/>
			
		</id>

		<property name="name"></property>
		<one-to-one name="room"/>
	</class>

</hibernate-mapping>

 

Hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
	<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_OnetoOne</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">seebig</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">false</property>
		
		<mapping resource="com/goby/Hibernate/bean/User.hbm.xml" />
		<mapping resource="com/goby/Hibernate/bean/Room.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 

测试代码:Test.java

package test;

import junit.framework.TestCase;

import org.hibernate.Session;

import com.goby.Hibernate.bean.HibernateUtils;
import com.goby.Hibernate.bean.Room;
import com.goby.Hibernate.bean.User;

/*
 * 一对一  (主键关联)
 * 
 */

public class test extends TestCase {
	
	public void testSave(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			User users = new User();
			users.setName("goby");
			
			Room room = new Room();
			room.setAddress("jiangmen");
			room.setUsers(users);
			
			session.save(room);
			
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
	
	/*
	 * 从ROOM类中获取数据
	 */
	public void testload(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Room room = (Room)session.load(Room.class, 1);
			System.out.println("room.address=" + room.getAddress());
			System.out.println("user.name=" + room.getUsers().getName());
			
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
	
	/*
	 * 从User类中获取数据
	 */
	public void testload2(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			User user = (User)session.load(User.class, 1);
			System.out.println("user.name ="+user.getName());
			System.out.println("room.address=" + user.getRoom().getAddress());
			
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
}

 

你可能感兴趣的:(sql,Hibernate,bean,mysql,xml)