hibernate笔记(二) 多对一 Many to One

User.java

package com.goby.Hibernate.bean;

public class User {
	private int id;
	private String name;
	private Group group;
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	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;
	}
}

 

 

Group.java

package com.goby.Hibernate.bean;

public class Group {
	
	private int id;
	
	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;
	}

	private String name;
}	

 

 

Use.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>
		<many-to-one name="group" column="groupid"
			class="com.goby.Hibernate.bean.Group" cascade="all"  />
	</class>
	
</hibernate-mapping>

 

Group.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="Group" table="t_group">
		
		<id name="id">
			<generator class="native"/>
		</id>
		
		<property name="name"></property>
		
	</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/hibernateManytoOne</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">true</property>
		
		<mapping resource="com/goby/Hibernate/bean/User.hbm.xml" />
		<mapping resource="com/goby/Hibernate/bean/Group.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 

测试代码:Test.java

package test;

import junit.framework.TestCase;

import org.hibernate.Session;

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

public class test extends TestCase {
	
	public void testSave(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Group group = new Group();
			group.setName("seebig");
			
			User user = new User();
			user.setName("goby");
			user.setGroup(group);
			
			session.save(user);
			
			
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testload(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			/*
			 * 相当于sql
			 * select * from t_user u left outer join t_group g on u.groupid=g.id where u.id=?
			 */
			User user = (User)session.get(User.class, 2);
			System.out.println("user.name="+user.getName());
			System.out.println("group.name="+user.getGroup().getName());
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
}

 

 

 

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