hibernate中的一对多(双向关系)

Dept

package com.wxh.dto;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
public class Dept {
	
	private int dno;
	private String dname;
	private String tel;	
	
	private Set emps=new HashSet();
	
	public Dept() {
	}	
	
	public Dept(int dno, String dname, String tel) {
		this.dno = dno;
		this.dname = dname;
		this.tel = tel;
	}

	@Id
	public int getDno() {
		return dno;
	}
	public void setDno(int dno) {
		this.dno = dno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}

	@OneToMany(fetch=FetchType.LAZY,cascade={CascadeType.ALL},mappedBy="dept")
	public Set getEmps() {
		return emps;
	}

	public void setEmps(Set emps) {
		this.emps = emps;
	}	
}

Emp

package com.wxh.dto;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Emp {
	
	private int eno;
	private String ename;
	private String job;
	private double sal;
	
	private Dept dept;	

	public Emp() {
	}	
	
	public Emp(int eno, String ename, String job, double sal) {
		super();
		this.eno = eno;
		this.ename = ename;
		this.job = job;
		this.sal = sal;
	}
	
	@Id
	public int getEno() {
		return eno;
	}

	public void setEno(int eno) {
		this.eno = eno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public double getSal() {
		return sal;
	}

	public void setSal(double sal) {
		this.sal = sal;
	}

	@ManyToOne(fetch=FetchType.EAGER,cascade={CascadeType.ALL})
	@JoinColumn(name="dno")
	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}	
		
}

Test

package test;

import org.hibernate.Session;

import com.wxh.dto.Dept;
import com.wxh.dto.Emp;
import com.wxh.sessionfactory.HibernateSessionFactory;

public class Test {

	public static void main(String[] args) {	
		
		Dept d=new Dept(10,"研发部","10086");
		Emp e1=new Emp(1,"张三","项目经理",12000);
		Emp e2=new Emp(2,"李四","测试经理",8000);
		Emp e3=new Emp(3,"王五","产品经理",22000);
		
		//指定员工所在部门
		e1.setDept(d);
		e2.setDept(d);
		e3.setDept(d);
		
		//将员工对象设置到集合中
		d.getEmps().add(e1);
		d.getEmps().add(e2);
		d.getEmps().add(e3);		
		
		Session session=HibernateSessionFactory.getSession();		
		session.beginTransaction();	
		session.save(d);
		session.getTransaction().commit();		
	}
}


hibernate.cfg.xml




	
		update
	
		org.hibernate.dialect.Oracle9Dialect
	
	
		jdbc:oracle:thin:@127.0.0.1:1521:orcl
	
	hibernate
	m123
	
		oracle.jdbc.driver.OracleDriver
	
	oracle
	true
	true	

	
			
	





你可能感兴趣的:(Hibernate)