一对多关联映射

单向关联

Employee类

package Mapping;

import java.util.Calendar;

public class Employee {

	private Long id;
	private String employeeNo;
	private String employeeName;
	private char gender;
	private Calendar birthdate;
	private double salary;
	
	public Employee() {
		
	}
	public Employee(String employeeNo, String employeeName, char gender,
			Calendar birthdate, double salary) {
		
		this.employeeNo = employeeNo;
		this.employeeName = employeeName;
		this.gender = gender;
		this.birthdate = birthdate;
		this.salary = salary;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getEmployeeNo() {
		return employeeNo;
	}
	public void setEmployeeNo(String employeeNo) {
		this.employeeNo = employeeNo;
	}
	public String getEmployeeName() {
		return employeeName;
	}
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public Calendar getBirthdate() {
		return birthdate;
	}
	public void setBirthdate(Calendar birthdate) {
		this.birthdate = birthdate;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}

Department类

package Mapping;

import java.util.Set;

public class Department {

	private Long id;
	private String deptName;
	private String telephone;
	private Set employees;	//引用员工的集合属性
	
	public Department() {}
	public Department(String deptName, String telephone, Set employees) {
		super();
		this.deptName = deptName;
		this.telephone = telephone;
		this.employees = employees;
	}
	
	//employees的getter和setter方法
	public Set getEmployees() {
		return employees;
	}
	public void setEmployees(Set employees) {
		this.employees = employees;
	}
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

}

Department.hbm.xml



    


		
		
			
		
		
		
		
		
			
		
			 
			
			
	

测试

package Mapping;

import java.util.*;

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

import util.HibernateUtil;

public class OneToManyUnidirectionalAssociation {

	public static void main(String[] args) {
		
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		Employee emp1 = new Employee("901","王小明",'M',new GregorianCalendar(1972,11,20),3500.00);
		Employee emp2 = new Employee("902","张大海",'F',new GregorianCalendar(1989,5,14),4800.00);
		Set employees = new HashSet();
		employees.add(emp1);
		employees.add(emp2);
		Department depart = new Department("软件开发部","3400222",employees);
		session.save(depart);
		tx.commit();
		
		//对于单向的一对多关联,查询时只能从"一"方导航到"多"方
		String query_str = "from Department d inner join d.employees e";
		Query query = session.createQuery(query_str);
		List list = query.list();
		for(int i=0;i

结果

一对多关联映射_第1张图片

一对多关联映射_第2张图片

一对多关联映射_第3张图片


双向关联

设置一对多双向关联,需在"多”方的类(如Employee类)中添加访问"一"方对象的属性和setter及getter方法。

在上面Employee类基础上添加如下代码:

private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}

"多”方的映射文件中使用元素定义多对一关联。

Employee.hbm.xml



  

	
		
			
		
		
		
		
		
		
		
		
				
		
	

此外,还需要把 Department.hbm.xml中的元素的inverse属性值设置为true。


测试

package Mapping;

import java.util.GregorianCalendar;
import java.util.List;

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

import util.HibernateUtil;


public class OneToManyBidirectionalAssociation {
	
	public static void main(String[] args) {
		
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		Department depart = new Department();
		depart.setDeptName("财务部");
		depart.setTelephone("112233");
		Employee emp1 = new Employee("1001","王小明",'男',new GregorianCalendar(1972,2,11),3500.00);
		Employee emp2 = new Employee("1002","张大海",'女',new GregorianCalendar(1989,11,14),4800.00);
		emp1.setDepartment(depart);
		emp2.setDepartment(depart);
		session.save(emp1);
		session.save(emp2);
		tx.commit();	//*若事务未提交数据库中没保存数据
		
		//查询员工及部门信息,这里用到了实体连接的功能,它是从"多"方导航到"一"方
		String queryString = "from Employee e inner join e.department d";
		Query query = session.createQuery(queryString);
		List list = query.list();
		for(int i=0;i

结果

一对多关联映射_第4张图片

一对多关联映射_第5张图片

一对多关联映射_第6张图片



你可能感兴趣的:(HIbernate)