Hibernate的級聯查詢和添加的分析


Hibernate的級聯查詢和添加的分析

 昨天有同學問我,級聯添加怎么做,我寫過級聯查詢,但是級聯添加,怎么做呢?怎么配置呢?昨天晚上就寫了試試,然后分享給大家。拿學生表和系別表來說。先看數據庫配置    首先是學生表studdent

學生表的depid是作為student的外鍵,主鍵是id,

下面是系別表department
這個沒什么好說的。 新建工程,然后進行兩個表的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 {// Fieldsprivate 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 accessorspublic 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里的 配置會多出這樣一句


而department.hbm.xml中會出現這樣
下面進行級聯查詢,直接調用這個方法, 查詢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();
}
那么我們就實現了這種級聯添加


此文僅供參考,若有錯,望請大家批評指正。



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com