欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate的级联查询和添加的分析

程序员文章站 2022-06-02 16:29:54
...

Hibernate的级联查询和添加的分析

 昨天有同学问我,级联添加怎么做,我写过级联查询,但是级联添加,怎么做呢?怎么配置呢?昨天晚上就写了试试,然后分享给大家。拿学生表和系别表来说。先看数据库配置   

首先是学生表studdent

Hibernate的级联查询和添加的分析

Hibernate的级联查询和添加的分析Hibernate的级联查询和添加的分析

学生表的depid是作为student的外键,主键是id,
 

下面是系别表department

Hibernate的级联查询和添加的分析

这个没什么好说的。

新建工程,然后进行两个表的hibernate的映射

package org.lyy.entity;

/**
 * Student entity. @author MyEclipse Persistence Tools
 */

public class Student implements java.io.Serializable {

	// Fields

	private Integer id;
	private Department department;//这里要添加系别表的对象
	private String stunumber;
	private String stupwd;
	private String stuname;
	private int  depid;
	
	// Constructors

	public int getDepid() {
		return depid;
	}

	public void setDepid(int depid) {
		this.depid = depid;
	}

	/** default constructor */
	public Student() {
	}

	/** minimal constructor */
	public Student(Department department) {
		this.department = department;
	}

	/** full constructor */
	public Student(Department department, String stunumber, String stupwd,
			String stuname) {
		this.department = department;
		this.stunumber = stunumber;
		this.stupwd = stupwd;
		this.stuname = stuname;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Department getDepartment() {
		return this.department;
	}

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

	public String getStunumber() {
		return this.stunumber;
	}

	public void setStunumber(String stunumber) {
		this.stunumber = stunumber;
	}

	public String getStupwd() {
		return this.stupwd;
	}

	public void setStupwd(String stupwd) {
		this.stupwd = stupwd;
	}

	public String getStuname() {
		return this.stuname;
	}

	public void setStuname(String stuname) {
		this.stuname = stuname;
	}

	public Student(String stunumber, String stupwd, String stuname,
			Department department) {
		super();
		this.id=id;
		this.stunumber = stunumber;
		this.stupwd = stupwd;
		this.stuname = stuname;
		this.department=department;
	}

}
package org.lyy.entity;

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

/**
 * Department entity. @author MyEclipse Persistence Tools
 */

public class Department implements java.io.Serializable {

	// Fields

	private Integer id;
	private String bm;
	private String mc;
	private String tell;
	private String leader;
	private Set students = new HashSet(0);

	// Constructors

	/** default constructor */
	public Department() {
	}

	/** full constructor */
	public Department(String bm, String mc, String tell, String leader,
			Set students) {
		this.bm = bm;
		this.mc = mc;
		this.tell = tell;
		this.leader = leader;
		this.students = students;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getBm() {
		return this.bm;
	}

	public void setBm(String bm) {
		this.bm = bm;
	}

	public String getMc() {
		return this.mc;
	}

	public void setMc(String mc) {
		this.mc = mc;
	}

	public String getTell() {
		return this.tell;
	}

	public void setTell(String tell) {
		this.tell = tell;
	}

	public String getLeader() {
		return this.leader;
	}

	public void setLeader(String leader) {
		this.leader = leader;
	}

	public Set getStudents() {
		return this.students;
	}

	public void setStudents(Set students) {
		this.students = students;
	}

}

看student.hbm.xml里的 配置会多出这样一句

Hibernate的级联查询和添加的分析

而department.hbm.xml中会出现这样Hibernate的级联查询和添加的分析

下面进行级联查询,直接调用这个方法, 查询student,就可以查到系院信息

public List<Student> getAllStudentList(){
		List<Student> list = null;
		Session session = HibernateSessionFactory.getSession();
		Query q = session.createQuery("from Student");
		list = q.list();
		session.close();
		return list;
<strong><span style="font-size:18px;">		}</span></strong>

或者直接建立个测试类进行测试一下

public class TestDepartmentStudent {
	public static void main(String[] args) {
		Session session = HibernateSessionFactory.getSession();
		session.beginTransaction();
		List<Student> list = session.createQuery("from Student").list();
		for(Student stu:list){
			System.out.println(" 学号:"+stu.getStunumber()+" ,姓名:"+stu.getStuname()+
					": , 系院编码: "+stu.getDepartment().getBm()+": , 系院名称: "+stu.getDepartment().getMc());
		}
		session.close();
	}
}

既然能查询,那么如何添加呢?

其实都一样,映射的时候得到系院对象,实现级联查询,那添加的时候,就能用系院对象,设置系院信息,那么这样就好办了

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String stunumber=request.getParameter("stunumber");
		String stupwd=request.getParameter("stupwd");
		String stuname=request.getParameter("stuname");
		String depid=request.getParameter("depid");
		
		Department  department=new Department();//得到系院对象
		department.setId( Integer.parseInt(depid));//这里用set赋值系院信息
		Student student=new Student(stunumber, stupwd, stuname, department);
		
		StudentDao stuDao=new StudentDao();
		stuDao.addStudent(student);
		response.sendRedirect("StudentListServlet");
	}

传参的时候,把department对象作为student的一个参数,然后出入一个student对象过去,进行存储

,(这里也可以设置department对象的其他属性)

	public void addStudent(Student student){
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = session.beginTransaction();
		session.save(student);
		tran.commit();
		session.close();
		}

那么我们就实现了这种级联添加

Hibernate的级联查询和添加的分析

此文仅供参考,若有错,望请大家批评指正。