4、对象模型映射

对象模型映射
OA里面有机构,我们需要创建一个机构的对象Organization,如下:
package com.oa.model;

import java.util.Set;

/**
 * @author [email protected]
 * @hibernate.class table="t_organization"
 */
public class Organization {
	/**
	 * @hibernate.id generator-class="native"
	 */
	private int id;
	/**
	 * @hibernate.property
	 */
	private String name;
	/**
	 * @hibernate.property
	 */
	private String sn;//机构编号
	/**
	 * @hibernate.property
	 */
	private String description;
	/**
	 * @hibernate.many-to-one column="pid"
	 */
	private Organization parent;//一个机构的上级机构
	/**
	 * @hibernate.set
	 * @hiberante.key  column="pid"
	 * @hibernate.one-to-many class="com.oa.model.Organization"
	 */
	private Set<Organization> children;//一个机构的子机构(一个机构有多个子机构,所以是一对多的关系)

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

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Organization getParent() {
		return parent;
	}

	public void setParent(Organization parent) {
		this.parent = parent;
	}

	public Set<Organization> getChildren() {
		return children;
	}

	public void setChildren(Set<Organization> children) {
		this.children = children;
	}
}

OA里面不但有机构Organization,还有人员Person,我们定义一个Person对象,它有一个Organization属性,如下:
package com.oa.model;
/**
 * @author [email protected]
 * @hibernate.class table="t_person"
 */
public class Person {
	/**
	 * @hibernate.id generator-class="native"
	 */
	private int id;
	/**
	 * @hibernate.property
	 */
	private String name;
	/**
	 * @hibernate.property
	 */
	private String sex;
	/**
	 * @hibernate.property
	 */
	private String address;
	/**
	 * @hibernate.property
	 */
	private String duty;
	/**
	 * @hibernate.property
	 */
	private String phone;
	/**
	 * @hibernate.property
	 */
	private String description;
	/**
	 * @hibernate.many-to-one
	 */
	private Organization org;

	public Organization getOrg() {
		return org;
	}

	public void setOrg(Organization org) {
		this.org = org;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

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

	public String getDuty() {
		return duty;
	}

	public void setDuty(String duty) {
		this.duty = duty;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
运行ant生成Person.hbm.xml和Organization.hbm.xml映射文件,如下:
Person.hbm.xml
<?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 table="t_person" name="com.oa.model.Person">
    <id access="field" name="id">
      <generator class="native"/>
    </id>
    <property name="name" access="field"/>
    <property name="sex" access="field"/>
    <property name="address" access="field"/>
    <property name="duty" access="field"/>
    <property name="phone" access="field"/>
    <property name="description" access="field"/>
  </class>
</hibernate-mapping>

Organization.hbm.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-

mapping-3.0.dtd">
<hibernate-mapping>
  <class table="t_organization" name="com.oa.model.Organization">
    <id access="field" name="id">
      <generator class="native"/>
    </id>
    <property name="name" access="field"/>
    <property name="sn" access="field"/>
    <property name="description" access="field"/>
    <many-to-one column="pid" access="field" name="parent"/>
    <set access="field" name="children">
      <key/>
      <one-to-many class="com.oa.model.Organization"/>
    </set>
  </class>
</hibernate-mapping>
接下来启动Tomcat服务器,在Tomcat启动时,它会读取Hibernate的映射和配置文件,同时在数据库里面生成表t_organization和表t_person。
测试是否在数据库确实能插入数据,这时需要添加junit-3.8.2.jar包。
测试,先写一个测试类的常用工具类:
package com.oa.model;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HiberanteUtils {
	private static SessionFactory factory = null;
	static {
		try {
			Configuration cfg = new Configuration().configure();
			factory = cfg.buildSessionFactory();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unused")
	public static SessionFactory getSessionFactory(){
		return factory;
	}
	public static Session getSession(){
		return factory.openSession();
	}
	public static void closeSession(Session session){
		if(session != null)
			if(session.isOpen())
				session.close();
	}
}
接着写测试类的测试方法:
package com.oa.model;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import junit.framework.Assert;
import junit.framework.TestCase;
public class OrganizationTest extends TestCase {
	public void testGetParent() {
		Session session = null;
		try {
			session = HiberanteUtils.getSession();
			session.beginTransaction();
			//childen --> parent //while load childen load parent
			Organization org2 = (Organization) session.load(Organization.class, "2");
			Assert.assertEquals("org2", org2.getName());
			Assert.assertEquals("org1", org2.getParent().getName());
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void testGetChildren() {
		Session session = null;
		try {
			session = HiberanteUtils.getSession();
			session.beginTransaction();
			//parent --> childen //while load parent load childen
			Organization org1 = (Organization) session.load(Organization.class, "1");
			Assert.assertEquals("org1", org1.getName());
			Set<Organization> set = org1.getChildren();
			for(Organization org : set){
				Assert.assertEquals(true, "org2org3org4".contains(org.getName()));
			}
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void testSetChildren() {
		Session session = null;
		try {
			session = HiberanteUtils.getSession();
			session.beginTransaction();
			
			Organization org1 = new Organization();
			org1.setName("org1");
			Set<Organization> set = new HashSet<Organization>();
			Organization org2 = new Organization();
			org2.setName("org2");
			set.add(org2);
			
			Organization org3 = new Organization();
			org3.setName("org3");
			set.add(org3);
			
			Organization org4 = new Organization();
			org2.setName("org4");
			set.add(org4);
			
			org1.setChildren(set);
			
			session.save(org1);
			session.save(org2);
			session.save(org3);
			session.save(org4);
			
			Assert.assertEquals("org1", ((Organization) session.load(Organization.class, "1")).getName());
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
测试成功。
注意hibernate.cfg.xml的<property name="hibernate.hbm2ddl.auto">update</property>,它只有两个取值,create和update。create会删除

以前的重新创建,update不会删除会更新。














你可能感兴趣的:(tomcat,Hibernate,xml,ant,JUnit)