private Criteria createCriteria() throws HibernateException {
   Session session = HibernateUtils.getSessionFactory().openSession();
   Criteria criteria = session.createCriteria(Emprestimo.class, "c");
   criteria.createAlias("c.bemMaterial", "bema");
   criteria.createAlias("bema.categoria", "cate");
   criteria.createAlias("cate.usuario", "u");
   criteria.add(Restrictions.eq("u.id", getUsrLogado().getId()));
   return criteria;
 }
示例#2
0
 public void savePet() {
   Session session = HibernateUtils.getSessionFactory().getCurrentSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     Cat cat = new Cat();
     cat.setId(1);
     cat.setName("cat");
     cat.setCatProperty("cat property");
     session.save(cat);
     tx.commit();
   } catch (Exception e) {
     if (tx != null) {
       tx.rollback();
     }
     throw new RuntimeException(e);
   }
 }
示例#3
0
 public void deletePet() {
   Session session = HibernateUtils.getSessionFactory().getCurrentSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     /**
      * 1. 这里需要注意, 如果此时1对应的一个对象时一个cat时, 也就是说此时cat中的pet_id=1, 那么 此时删除是会报错的, 因为此时删除pet, 但是外键约束,
      * 所以不能删除
      */
     //			Pet pet = new Pet();
     //			pet.setId(1);
     //			session.delete(pet);
     tx.commit();
   } catch (Exception e) {
     if (tx != null) {
       tx.rollback();
     }
     throw new RuntimeException(e);
   }
 }
示例#4
0
 public void getPet() {
   Session session = HibernateUtils.getSessionFactory().getCurrentSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     // Cat cat = (Cat) session.get(Cat.class, 1);
     // System.out.println(cat.getCatProperty());
     /**
      * 当使用pet来查找的时候, 虽然一个id肯定只能时对应于一个cat, 或者时一个dog但是它此时会连接所以 的pet dog cat表进行查找,
      * 因为此时它并不知道为1的pet到底是一个dog还是一个cat, 所以它会全部 都查找出来, 再根据结果来创建dog或者是cat。如果此时什么也不是, 那么此时的对象为一个pet。
      */
     Pet pet = (Pet) session.get(Pet.class, 1);
     System.out.println(pet.getClass());
     tx.rollback();
   } catch (Exception e) {
     if (tx != null) {
       tx.rollback();
     }
     throw new RuntimeException(e);
   }
 }