a test code
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;
/*
* One-way many-to-one (student--->class)
* In fact, two-way one-to-many has been established, also known as two-way many-to-one
* It is convenient for students to find the corresponding class information, and it is also easy to find other included student information in the right class
*/
public class Test02 {
public static void main(String[] args) {
//save();
findGradeByStudents();
}
//Query the class information of the student
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);
}
}
Two test results
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, Xiao Mumu, male
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 class, Java software development class