一对多(部门and员工)

package com.zchen.user.domain;

import java.util.HashSet;
import java.util.Set;

public class Department {

	private int id;
	private String name;
	private Set<Employee> employees = new HashSet<Employee>();
	
	public Set<Employee> getEmployees() {
		return employees;
	}
	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}
	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;
	}
	
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  
<hibernate-mapping package="com.zchen.user.domain">
	<class name="Department" table="`department`">
	  	<id name="id" column="`id`">
		  <generator class="native">
		  </generator>
	  	</id>
	  	<property name="name" unique="true"/>
	  	<set name="employees">
	  		<key column="depart_id"></key> <!-- 外键 -->
	  		<one-to-many class="Employee"/>
	  	</set>
	</class>
</hibernate-mapping>

 

package com.zchen.user.domain;

public class Employee {

	private int id;
	private String name;
	private Department department;
	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 Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	
}

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  
<hibernate-mapping package="com.zchen.user.domain">
	<class name="Employee" table="`employee`">
	  	<id name="id" column="`id`">
		  <generator class="native">
		  </generator>
	  	</id>
	  	<property name="name"/>
	  	<many-to-one name="department" column="depart_id"></many-to-one>
	</class>
	
</hibernate-mapping>

 

测试:

package test;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zchen.user.dao.HibernateUtil;
import com.zchen.user.domain.Department;
import com.zchen.user.domain.Employee;

public class one2Many {
	public static void main(String[] args) {
		add();
		query(1);
	}
	public static Department add(){
		Department depart = new Department();
		Employee emp1 = new Employee();
		Employee emp2 = new Employee();
		Session session = null;
		Transaction tx = null;
		try {
			depart.setName("事业部");
			
			emp1.setDepartment(depart);//对象模型 建立两个对象的关联
			emp1.setName("阿里郎");
			
			emp2.setDepartment(depart);//对象模型 建立两个对象的关联
			emp2.setName("草原狼");
			
			/*Set<Employee> emps = new HashSet<Employee>();
			emps.add(emp1);
			emps.add(emp2);
			depart.setEmployees(emps);*/
			
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			//session.save(depart);
			//session.save(emp);
			session.save(emp1);
			session.save(emp2);
			session.save(depart);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session != null){
				session.close();
			}
		}
		return depart;
	}
	
	static Department query(int departId){
		Session session = null;
		Transaction tx = null;
		Department depart = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			depart = (Department)session.get(Department.class, departId);
			System.out.println("emp size"+depart.getEmployees().size());
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session != null){
				session.close();
			}
		}
		return depart;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值