用JUnit来测试增加和查询出错
用main函数测试成功但是用JUnit测试就失败是为什么呢?
package hibernate;
public class TestOne2Many {
// public static void main(String[] args) {
// //add();
// findStudentsByGrade();
// }
@Test
public static void add(){
- //将学生添加到班级
Grade g=new Grade("Java一班","Java软件开发一班");
Student s1=new Student("小明", "男");
Student s2=new Student("小红", "女");
g.getStudents().add(s1);
g.getStudents().add(s2);
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
session.save(g);
session.save(s1);
session.save(s2);
tx.commit();
HibernateUtil.closeSession(session);
}
@Test
public static void findStudentsByGrade(){
Session session=HibernateUtil.getSession();
Grade grade=(Grade) session.get(Grade.class, 1);
System.out.println(grade.getGname()+","+grade.getGdesc());
Set<Student> s=grade.getStudents();
for (Student student : s) {
System.out.println(student.getSname());
}
}
}