hibernate多对一,一对多,多对多,一对一举例案例

1.项目结构

hibernate多对一,一对多,多对多,一对一举例案例_第1张图片

2. 主配置文件 hibernate.cfg.xml




	
	
		
		
		org.hibernate.dialect.MySQLDialect
		com.mysql.jdbc.Driver
		jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8
		root
		root
		
		
		
		true
		
		true
		
		thread
		
		 
		
		
		
		
		
		
	

3. 多对一相关的举例配置文件Subject.hbm.xml





  
  
  	
  	
  	
  		
  	
  	
  	
  	
  	
  	
  		
  		
  	
  
  

4. 一对多相关的举例配置文件Grade.hbm.xml





  
  
  	
  	
  	
  		
  	
  	
  	
  	
  	
  	
  		
  		
  		
  		
  	
  
  

5. 多对多相关的举例配置文件Students.hbm.xml和Teachers.hbm.xml





  
  
  	
  	
  	
  		
  	
  	
  	
  	
  	
  	
  	
  	
  		
  		
  	
  
  





  
  
  	
  	
  	
  		
  	
  	
  	
  	
  	
  	
  		
  		
  	
  
  

6. 一对一相关的举例配置文件Students.hbm.xml和StudentsInfo.hbm.xml





  
  
  	
  	
  	
  		
  	
  	
  	
  	
  	
  	
  	
  	
  		
  		
  	
  
  





  
  
  	
  	
  	
  		
  	
  	
  	
  	
    
  
  

7. 相关实体类Grade.java,Students.java,StudentsInfo.java,Subject.java,Teachers.java

package com.zhq.entity;

import java.util.Set;

public class Grade {
	private int gradeId;
	private String gradeName;
	private Set subjects;
	public int getGradeId() {
		return gradeId;
	}
	public void setGradeId(int gradeId) {
		this.gradeId = gradeId;
	}
	public String getGradeName() {
		return gradeName;
	}
	public void setGradeName(String gradeName) {
		this.gradeName = gradeName;
	}
	
	public Set getSubjects() {
		return subjects;
	}
	public void setSubjects(Set subjects) {
		this.subjects = subjects;
	}
	@Override
	public String toString() {
		return "Grade [gradeId=" + gradeId + ", gradeName=" + gradeName + "]";
	}
	
	
}
package com.zhq.entity;

import java.util.Set;

public class Students {
	private int studentId;
	private String studentName;
	private StudentsInfo studentInfo;
	private Set teachers;
	public int getStudentId() {
		return studentId;
	}
	public void setStudentId(int studentId) {
		this.studentId = studentId;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public StudentsInfo getStudentInfo() {
		return studentInfo;
	}
	public void setStudentInfo(StudentsInfo studentInfo) {
		this.studentInfo = studentInfo;
	}
	public Set getTeachers() {
		return teachers;
	}
	public void setTeachers(Set teachers) {
		this.teachers = teachers;
	}
	
}
package com.zhq.entity;

public class StudentsInfo {
	private int studentInfoId;
	private String sex;
	private Students student;
	public int getStudentInfoId() {
		return studentInfoId;
	}
	public void setStudentInfoId(int studentInfoId) {
		this.studentInfoId = studentInfoId;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Students getStudent() {
		return student;
	}
	public void setStudent(Students student) {
		this.student = student;
	}
	

}

package com.zhq.entity;

public class Subject {
	private int subjectNo;
	private String subjectName;
	private int classHour;
	//年级编号外键,实体类采用外键对象来处理 多对一
	private Grade grade;
	public int getSubjectNo() {
		return subjectNo;
	}
	public void setSubjectNo(int subjectNo) {
		this.subjectNo = subjectNo;
	}
	public String getSubjectName() {
		return subjectName;
	}
	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}
	public int getClassHour() {
		return classHour;
	}
	public void setClassHour(int classHour) {
		this.classHour = classHour;
	}
	public Grade getGrade() {
		return grade;
	}
	public void setGrade(Grade grade) {
		this.grade = grade;
	}
	@Override
	public String toString() {
		return "Subject [subjectNo=" + subjectNo + ", subjectName=" + subjectName + ", classHour=" + classHour + ", gradeName=" + grade.getGradeName() +  "]";
	}
	
}
package com.zhq.entity;

import java.util.Set;

public class Teachers {
	private int teacherId;
	private String teacherName;
	private Set students;
	public int getTeacherId() {
		return teacherId;
	}
	public void setTeacherId(int teacherId) {
		this.teacherId = teacherId;
	}
	public String getTeacherName() {
		return teacherName;
	}
	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}
	public Set getStudents() {
		return students;
	}
	public void setStudents(Set students) {
		this.students = students;
	}
	
}
8. 测试功能所用到相关的类GradeController.java,StudentController.java,SubjectController.java,TeacherController.java
package com.zhq.controller;

import java.util.List;

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

import com.zhq.entity.Grade;
import com.zhq.entity.Subject;
import com.zhq.util.SessionFactoryUtil;

public class GradeController {

	public static void main(String[] args) {
		/*增
		 * Grade g=new Grade();
		   g.setGradeName("大三");
		   add(g);
		 */
		
		
           read();
       
		
		/*通过主键查询某个
		   readOne(1);
		 */
		
		/*通过主键删除
		  delete(29);
		 */
		
		/*通过主键修改
		 * Grade g=new Grade();
		   g.setGradeId(27);
		   g.setGradeName("大三");
		   update(g);
		 */
		
	}
	//查全部
	public static void read() {
		//获取连接Session,此对象用于操作数据库对象
		Session session=SessionFactoryUtil.getSession();
		//开启事务,如果是增删改的操作,必须要提交事务
		session.beginTransaction();
		//根据Session连接对象操作数据库,"from Grade"是HQL语言,注意:Grade是表示实体对象
		Query query=session.createQuery("from Grade");
		@SuppressWarnings("unchecked")
		List grades=query.list();
		for(Grade grade:grades){
			System.out.println(grade);
			for(Subject subject:grade.getSubjects()) {
				System.out.println(subject);
			}
		}
		session.close();
	}
	//查单个对象
	public static void readOne(int gradeId) {
		//获取连接Session,此对象用于操作数据库对象
		Session session=SessionFactoryUtil.getSession();
		//开启事务,如果是增删改的操作,必须要提交事务
		session.beginTransaction();
		Grade g=(Grade) session.get(Grade.class, gradeId);
		if(g!=null) {
			System.out.println(g);	
		}else {
			System.out.println("没有找到相关数据");
		}
	}
	//增
	public static void add(Grade g) {
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			session.save(g);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
		}
	}
	//删
	public static void delete(int gradeId) {
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			Grade g=(Grade) session.get(Grade.class, gradeId);
			if(g!=null) {
				session.delete(g);	
			}else {
				System.out.println("没有找到相关数据");
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
		}
	}
	//改
	public static void update(Grade g) {
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			session.update(g);	
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
		}
	}
}
package com.zhq.controller;

import java.util.List;

import org.hibernate.Session;


import com.zhq.entity.Students;
import com.zhq.entity.StudentsInfo;
import com.zhq.entity.Teachers;
import com.zhq.util.SessionFactoryUtil;

public class StudentController {

	public static void main(String[] args) {
		
		add();
		read();
	}

	private static void add() {
		Session session=SessionFactoryUtil.getSession();
		session.beginTransaction();
		Students student=new Students();
		StudentsInfo studentInfo=new StudentsInfo();
		student.setStudentName("大熊");
		session.save(student);
		studentInfo.setSex("男");
		studentInfo.setStudent(student);
		session.save(studentInfo);
		session.getTransaction().commit();	
	}
	private static void read() {
		Session session=SessionFactoryUtil.getSession();
		session.beginTransaction();
		@SuppressWarnings("unchecked")
		List list=session.createQuery("from Students").list();
	    for(Students student:list) {
	    	System.out.println(student.getStudentName()+"的老师:");
	    	for(Teachers teacher:student.getTeachers()) {
	    		System.out.print(teacher.getTeacherName()+",");
	    	}
	    	System.out.println();
	    }
	    session.getTransaction().commit();
	}

}
package com.zhq.controller;

import java.util.List;

import org.hibernate.Session;

import com.zhq.entity.Subject;
import com.zhq.util.SessionFactoryUtil;

public class SubjectController {

	public static void main(String[] args) {
		read();

	}

	private static void read() {
		Session session=SessionFactoryUtil.getSession();
		session.beginTransaction();
		@SuppressWarnings("unchecked")
		List list=session.createQuery("from Subject").list();
	    for(Subject subject:list) {
	    	System.out.println(subject);
	    }
	    session.getTransaction().commit();
	}

}
package com.zhq.controller;

import java.util.List;

import org.hibernate.Session;

import com.zhq.entity.Students;
import com.zhq.entity.Teachers;
import com.zhq.util.SessionFactoryUtil;

public class TeacherController {

	public static void main(String[] args) {
		read();
	}
	private static void read() {
		Session session=SessionFactoryUtil.getSession();
		session.beginTransaction();
		@SuppressWarnings("unchecked")
		List list=session.createQuery("from Teachers").list();
	    for(Teachers teacher:list) {
	    	System.out.println(teacher.getTeacherName()+"的学生:");
	    	for(Students student:teacher.getStudents()) {
	    		System.out.print(student.getStudentName()+",");
	    	}
	    	System.out.println();
	    }
	    session.getTransaction().commit();
	}

}








你可能感兴趣的:(后端框架)