hibernate 一对多配置

测试代码
package test.org.test.hiberante.pojo;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.test.hiberante.pojo.Role;
import org.test.hiberante.pojo.User;

import junit.framework.TestCase;

public class TestHibernate extends TestCase {
	
	
	SessionFactory sf;
	
	protected void setUp() throws Exception {
		sf = new Configuration().configure().buildSessionFactory();   
		super.setUp();
	}

	protected void tearDown() throws Exception {
		sf.close();
		super.tearDown();
	}
	
	public void testCrud(){
		

		Session session = sf.openSession();
		
		User user = new User();
		
//		user.setId(12);
		user.setName("zhangsan");
		
		Transaction tx = session.beginTransaction();
		session.save(user);
		tx.commit();
		session.close();
	}

	
	public void testS(){
		
		Session session = sf.openSession();
		
//		User user = (User) session.load(User.class, new Long(3));
		
		
		
//		Role role = new Role();
		Role role = (Role) session.load(Role.class, new Long(1));
		User user = new User();
		
		user.setName("zhangsan");
		user.setRole(role);
		
//		role.setRname("nomal");
		
		Transaction tx = session.beginTransaction();
		session.save(user);
		tx.commit();
		session.close();
		
//		if(null != user){
//			System.out.println(user.getName());
//		}else{
//			System.out.println("user is not here");
//		}
//		
		
		
	}
	
	
	public void testR(){
		Session session = sf.openSession();
		
//		User user = (User) session.load(User.class, new Long(1));
		
//		System.out.println(user);
		
		Role role = (Role) session.load(Role.class, new Long(1));
		
		
		
		System.out.println(role);
		
		
//		Role role = new Role();
//		Role role = (Role) session.load(Role.class, new Long(1));
//		User user = new User();
//		
//		user.setName("zhangsan");
//		user.setRole(role);
		
//		role.setRname("nomal");
//		
//		Transaction tx = session.beginTransaction();
//		session.save(user);
//		tx.commit();
//		session.close();
		
//		if(null != user){
//			System.out.println(user.getName());
//		}else{
//			System.out.println("user is not here");
//		}
	}
	
}






















































<?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="org.test.hiberante.pojo.User" table="user" lazy="true">
		<id column="id" name="id" type="long" unsaved-value="null">
			<generator class="native"/>
		</id>
		<property column="name" length="20" name="name"  type="string"/>
		
		<many-to-one name="role">
			<column name="rid"/>
		</many-to-one>
	</class>
	
</hibernate-mapping>

<?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="org.test.hiberante.pojo.Role" table="role" lazy="true">
		<id column="id" name="id" type="long" unsaved-value="null">
			<generator class="native"/>
		</id>
		<property column="rname" length="20" name="rname"  type="string"/>
		<set name="users">
			<key column="rid"/>
			<one-to-many class="org.test.hiberante.pojo.User"/>
		</set>
	</class>
</hibernate-mapping>


package org.test.hiberante.pojo;

public class User {
	
	private long id;
	private String name;
	private Role role;
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String toString(){
		return name+" : "+id;
	}
}


package org.test.hiberante.pojo;

import java.util.Set;

public class Role {
	
	private long id;
	private String rname;
	private Set users;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getRname() {
		return rname;
	}
	public void setRname(String rname) {
		this.rname = rname;
	}
	
	public String toString(){
		return rname+" : "+id;
	}
	public Set getUsers() {
		return users;
	}
	public void setUsers(Set users) {
		this.users = users;
	}
}

你可能感兴趣的:(java,Hibernate,.net,xml,JUnit)