一 测试代码
package com.imooc.test;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.imooc.entity.Grade;
import com.imooc.entity.Student;
import com.imooc.util.HibernateUtil;
/*
* 单向多对一(学生--->班级)
* 实际上已经建立了双向一对多,也称双向多对一
* 既可以方便地有学生找到对应的班级信息,也可以方便地右班级查找其他所包含的学生信息
*/
public class Test02 {
public static void main(String[] args) {
//save();
findGradeByStudents();
}
//查询学生所在的班级信息
public static void findGradeByStudents(){
Session session=HibernateUtil.getSession();
Student student=(Student) session.get(Student.class, 1);
System.out.println(student.getSid()+","+student.getSname()+","+student.getSex());
Grade g= student.getGrade();
System.out.println(g.getGid()+","+g.getGname()+","+g.getGdesc());
HibernateUtil.closeSession(session);
}
}
二 测试结果
Hibernate: select student0_.sid as sid1_1_0_, student0_.sname as sname2_1_0_, student0_.sex as sex3_1_0_, student0_.gid as gid4_1_0_ from student student0_ where student0_.sid=?
1,小慕慕,男
Hibernate: select grade0_.gid as gid1_0_0_, grade0_.gname as gname2_0_0_, grade0_.gdesc as gdesc3_0_0_ from grade grade0_ where grade0_.gid=?
1,Java一班,Java软件开发一班