示例#1
0
 /**
  * 查询单个对象
  *
  * @param id
  */
 private static void getStu() {
   Session session = null;
   Transaction trans = null;
   try {
     session = HibernateSessionFactory.getSession();
     trans = session.beginTransaction();
     // 确认最多只有一条才用
     Student stu =
         (Student) session.createQuery("from Student where id='20040001'").uniqueResult();
     trans.commit();
     System.out.println(stu.getSname());
   } catch (Exception e) {
     e.printStackTrace();
     if (trans != null) {
       trans.rollback();
     }
     throw new RuntimeException(e.getMessage());
   } finally {
     if (session != null && session.isOpen()) {
       HibernateSessionFactory.closeSession();
     }
   }
 }
示例#2
0
 /** 检索类的全部属性 */
 private static void getall() {
   // 1、检索所有学生信息
   List<Student> stus = hql("from Student");
   // for循环取出信息
   System.out.println("for循环取出list:");
   for (Student student : stus) {
     System.out.println(student.getSid() + " " + student.getSname());
   }
   // 迭代器取
   System.out.println("迭代器取出list:");
   Iterator<Student> iterator = stus.iterator();
   while (iterator.hasNext()) {
     Student s = iterator.next();
     System.out.println(s.getSid() + " " + s.getSname());
   }
 }
示例#3
0
 /** 查询计算机系和外语系的学生 */
 private static void getsome() {
   List<Student> stus = hql("from Student where sdept in('计算机系','外语系')");
   for (Student student : stus) {
     System.out.println(student.getSid() + " " + student.getSname());
   }
 }