1 package model; 2 3 import java.io.Serializable; 4 5 //多对一 6 //学生表 n : 1班级表 7 //在多的一方定义一方引用 8 public class Student implements Serializable { 9 private int sid; 10 private String sname; 11 private String sex; 12 private int gid;//班级外键 13 14 //一方的引用 15 private Grade grade; 16 17 public Student(){ 18 19 } 20 21 public Student(int sid) { 22 super(); 23 this.sid = sid; 24 } 25 26 public int getGid() { 27 return this.getGrade().getGid(); 28 } 29 30 public void setGid(int gid) { 31 this.gid = gid; 32 } 33 34 public Grade getGrade() { 35 return grade; 36 } 37 38 public void setGrade(Grade grade) { 39 this.grade = grade; 40 } 41 42 public Student(String sname, String sex) { 43 super(); 44 this.sname = sname; 45 this.sex = sex; 46 } 47 48 public int getSid() { 49 return sid; 50 } 51 public void setSid(int sid) { 52 this.sid = sid; 53 } 54 public String getSname() { 55 return sname; 56 } 57 public void setSname(String sname) { 58 this.sname = sname; 59 } 60 public String getSex() { 61 return sex; 62 } 63 public void setSex(String sex) { 64 this.sex = sex; 65 } 66 67 }
//实体类建立好之后,可以通过hibernate插件自动生成关系
1 <!-- 单向多对一配置,在多方关联一方外键 --> 2 <many-to-one name="grade" class="model.Grade" access="field" fetch="join"> 3 <column name="gid" /> 4 </many-to-one>
//班级表
1 package model; 2 3 import java.io.Serializable; 4 import java.util.Set; 5 6 public class Grade implements Serializable{ 7 private int gid; 8 private String gname; 9 private String gdesc; 10 11 private Set<Student> student; //定义学生集合,一对多,在一方定义多方集合 12 13 public Set<Student> getStudent() { 14 return student; 15 } 16 17 public void setStudent(Set<Student> student) { 18 this.student = student; 19 } 20 21 public Grade(){ 22 23 } 24 25 public Grade(int gid, String gname, String gdesc) { 26 super(); 27 this.gid = gid; 28 this.gname = gname; 29 this.gdesc = gdesc; 30 } 31 32 public Grade(int gid) { 33 super(); 34 this.gid = gid; 35 } 36 37 public Grade(String gname, String gdesc) { 38 super(); 39 this.gname = gname; 40 this.gdesc = gdesc; 41 } 42 43 public int getGid() { 44 return gid; 45 } 46 47 public void setGid(int gid) { 48 this.gid = gid; 49 } 50 51 public String getGname() { 52 return gname; 53 } 54 55 public void setGname(String gname) { 56 this.gname = gname; 57 } 58 59 public String getGdesc() { 60 return gdesc; 61 } 62 63 public void setGdesc(String gdesc) { 64 this.gdesc = gdesc; 65 } 66 67 }
//班级表关系配置
1 <!-- 配置一对多关系,设置多方集合 --> 2 <set name="student" inverse="true" cascade="all"> 3 <key column="gid"></key> 4 <one-to-many class="model.Student"/> 5 </set>
//测试类
1 package test; 2 3 import java.util.Set; 4 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 8 import util.HibernateUtil; 9 import model.Grade; 10 import model.Student; 11 12 public class Test { 13 public static void main(String[] args) { 14 // save(); 15 // findGradeByStudent(1); 16 findStudentByGrade(1); 17 } 18 19 //保存,比较鸡肋,主要用于关联查询,删除等 20 public static void save(){ 21 Grade grade = new Grade(1); 22 Student student2 = new Student("廖海2", "男"); 23 24 //设置多对一关联关系(外键) 25 student2.setGrade(grade); 26 27 //执行操作 28 Session session = HibernateUtil.getSession(); 29 Transaction tx = session.beginTransaction();//开启事务 30 session.save(student2); 31 System.out.println("save is ok!"); 32 tx.commit(); //事务落地 33 HibernateUtil.closeSession();//关闭连接 34 35 } 36 37 //根据学生id查询出学生所在班级信息 38 public static void findGradeByStudent(int sid){ 39 //执行操作 40 Session session = HibernateUtil.getSession(); 41 //查询学生 42 Student student1 = (Student) session.get(Student.class, new Integer(sid)); //使用Integer封装,类才能序列化,变量不能序列化 43 System.out.println(student1.getSname()); 44 //输出班级信息 45 System.out.println(student1.getSid() + student1.getGrade().getGname() + student1.getGrade().getGdesc()); 46 //关闭连接 47 HibernateUtil.closeSession(); 48 } 49 50 //根据学生班级id,查询出班级中所有学生的信息 51 public static void findStudentByGrade(int gid){ 52 Session session = HibernateUtil.getSession(); 53 //查询班级 54 Grade grade = (Grade) session.get(Grade.class, gid); 55 //查询班级中的学生集合 56 Set<Student> studentSet = grade.getStudent(); 57 for(Student student : studentSet){ 58 System.out.println("班级id:" + student.getGid()); 59 System.out.println("学生id:" + student.getSid()); 60 System.out.println("学生姓名:" + student.getSname()); 61 System.out.println("学生性别:" + student.getSex()); 62 System.out.println("============================="); 63 } 64 65 } 66 67 }
//设置好关系之后,运行hibernate,hibernate会自动帮我们在数据库中为表关联外键。
原文:http://www.cnblogs.com/liaohai/p/6523171.html