Hibernate双向多对一,和一对多总结

Hibernate双向多对一,和一对多总结

双向多对一,一对多

相关难点mappedBy的相关注意事项

1.mappedBY是会存在于OnetoMany,OnetoOne,ManytoMany中,而在ManytoOne中是不存在的。

2.在mappedBy和Joincolumn是一对对立方(关系体现在Joincolumn是二张表中关系的拥有者,而mappedBy是关系的维护者)

3.mappedBy是由One的一方指向Many的一方,name属性的设置是由One一方所属性的对象来作为参数的。(并且实际上是Joincolumn中name属性的外键约束,也就是name的值,就就是外键)。

mappedBy和Joincolumn的区别:

1Joincolumn是主体的关联属性名,也是二张表关系的拥有者(其列名的规范为:主体的关联属性名+下划线+被关联端的主键列名(关联外键名称)example:dept_id)

第一步导入相关的jar包

第二步配置Hibernate.cfg.xml






	
		
		com.mysql.jdbc.Driver
		jdbc:mysql://localhost:3306/houseforrent?characterEncoding=UTF-8
		root
		
		
		5
		
		org.hibernate.dialect.MySQL5Dialect
		
		true
		
		update
		
		true
		
		
		
		
		
		
		
	


第三步创建实体类(这里我们以企业员工表和部门表作为案例)

部门表

package org.xiaoyi.hibernate.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity                 //通过Hibernate映射使普通的实体类生成关系模型的实体类
@Table(name = "t_dept") //创建数据库中表的名字,如果不写,将默认为以实体类的名字为数据库表的名字
public class Dept {     

	// 部门编号
	@Id   //设置为主键
	private Integer deptno;
	// 部门名称
	private String dname;

	// 部门对应的员工集合属性
	// mapperby配置在主控制方表示自己的属性 
	@OneToMany(mappedBy="dept",cascade=CascadeType.ALL,orphanRemoval=true)
	private List emps;

	public List getEmps() {
		return emps;
	}

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

	public Integer getDeptno() {
		return deptno;
	}

	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}

	@Column(length = 50)
	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	@Override
	public String toString() {
		return "Dept [deptno=" + deptno + ", dname=" + dname + "]";
	}

}

员工表

package org.xiaoyi.hibernate.domain;

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;


@Entity
@Table(name="t_emp")
public class Emp {

	// 编号
	@Id     //主键
	@GeneratedValue(strategy=GenerationType.IDENTITY)  //主键生成策略
	private Integer empno;
	// 姓名
	private String ename;
	// 工作
	@Column(name="job",nullable=false,length=50)
	private String job;
	
	// 奖金(使用工资1.2)
	@Transient     //不会参加sql语句的相关查询
	private Double comm;
	
	// 工资
	@Basic
	private Float sal;
	// 入职日期
	@Temporal(TemporalType.DATE)   //时间戳
	private Date hiredate;
	// 员工对应的部门属性
	@ManyToOne                    //多对一
	@JoinColumn(name="deptno",foreignKey=@ForeignKey(name = "dept_no_fk") )    //name值为外键值
	private Dept dept;
	
	

	public Integer getEmpno() {
		return empno;
	}

	public void setEmpno(Integer empno) {
		this.empno = empno;
	}

	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 Float getSal() {
		return sal;
	}

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

	public Date getHiredate() {
		return hiredate;
	}

	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}

	public Double getComm() {
		return comm;
	}

	public void setComm(Double comm) {
		this.comm = comm;
	}

	
	//===============员工对应部门属性=================//
	
	
	public Dept getDept() {
		return dept;
	}

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

	@Override
	public String toString() {
		return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", comm=" + comm + ", sal=" + sal
				+ ", hiredate=" + hiredate + "]";
	}
}

二表OnetoMany和ManytoOne相关的测试代码

package org.xiaoyi.hibernate.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.xiaoyi.hibernate.domain.Dept;
import org.xiaoyi.hibernate.domain.Emp;
import org.xiaoyi.hibernate.util.HibernateUtil;

public class Many2OneTest {

	public static void main(String[] args) {

		//addDeptAndEmp();
		
		//getEmpAndDept();
		
		getDeptAndEmps();

	}

	// 添加部门同时 添加一个员工
	public static void addDeptAndEmp() {

		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		Dept dept = new Dept();
		dept.setDeptno(10);
		dept.setDname("研发部");

		Emp emp = new Emp();
		emp.setEname("张三");
		emp.setJob("java开发");
		emp.setSal(10000F);
		emp.setHiredate(new Date());

		// 建立 对象模型关联关系
		// 指定 员工对应的部门
		emp.setDept(dept);

		session.save(dept);
		session.save(emp);

		tx.commit();
		session.close();
	}
	
	
	// 获得一个员工同时获得员工对应的部门信息
	public static void getEmpAndDept(){
		
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		Emp emp = session.get(Emp.class, 1);
		System.out.println(emp.toString());
		
		Dept dept = emp.getDept();
		System.out.println(dept.toString());
		
		tx.commit();
		session.close();
	}
	
	
	// 获得部门同时获得此部门所有的员工列表
	public static void getDeptAndEmps(){
		
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		Dept dept = session.get(Dept.class, 10);
		System.out.println(dept.toString());
		
		List list = dept.getEmps();
		for(Emp emp:list){
			System.out.println(emp.toString());
		}
		
		tx.commit();
		session.close();
	}
	
}

你可能感兴趣的:(Hibernate)