测试查询:
static Employee query(int empId) {
    Session s = null;
    Transaction tx = null;
    try {
      s = HibernateUtils.getSession();
      tx = s.beginTransaction();
      Employee emp = (Employee) s.get(Employee.class, empId);
      System.out.println(emp.getDepart().getName());
      tx.commit();
      return emp;
    } catch (HibernateException e) {
      e.printStackTrace();
    } finally {
      if (s != null) {
        s.close();
      }
    }
    return null;
  }
 
注意:查询时,不能用外部拿到的employee来获取到对于的department名称.
会报no proxy的异常.还查不到原因,
Hibernate.initialize(emp.getDepart())可以拿到.