@Test public void testFindByLastname() throws Exception { String hql = "from Person p where p.lastname = :lastname"; String lastname = "Smith"; List<Person> theSmiths = new ArrayList<Person>(); theSmiths.add(new Person("Alice", lastname)); theSmiths.add(new Person("Billy", lastname)); theSmiths.add(new Person("Clark", lastname)); expect(factory.getCurrentSession()).andReturn(session); expect(session.createQuery(hql)).andReturn(query); expect(query.setParameter("lastname", lastname)).andReturn( query); expect(query.list()).andReturn(theSmiths); replay(factory, session, query); HibernatePersonDao dao = new HibernatePersonDao(); dao.setSessionFactory(factory); assertEquals(theSmiths, dao.findByLastname(lastname)); verify(factory, session, query); }
@SuppressWarnings("unchecked") public List<Person> findByLastname(String name) { try { Session session = sessionFactory.getCurrentSession(); String hql = "from Person p where p.lastname = :lastname"; Query query = session.createQuery(hql); query.setParameter("lastname", name); return query.list(); } catch (HibernateException e) { throw new RuntimeException(e); } }
HibernatePersonDao implements PersonDao { @SuppressWarnings("unchecked") public List<Person> findByLastname(String name) { try { Session session = sessionFactory.getCurrentSession(); String hql = "from Person p where p.lastname = :lastname"; Query query = session.createQuery(hql); query.setParameter("lastname", name); return query.list(); } catch (HibernateException e) { throw new RuntimeException(e); } } }
HibernatePersonDao implements PersonDao { @SuppressWarnings("unchecked") public List<Person> findByLastname(String name) { try { Session session = sessionFactory.getCurrentSession(); String hql = "from Person p where p.lastname = :lastname"; Query query = session.createQuery(hql); query.setParameter("lastname", name); return query.list(); } catch (HibernateException e) { throw new RuntimeException(e); } }  }
HibernatePersonDao implements PersonDao { @SuppressWarnings("unchecked") public List<Person> findByLastname(String name) { try { Session session = sessionFactory.getCurrentSession(); String hql = "from Person p where p.lastname = :lastname"; Query query = session.createQuery(hql); query.setParameter("lastname", name); return query.list(); } catch (HibernateException e) { throw new RuntimeException(e); } }  void setSessionFactory(SessionFactory sessionFactory); @SuppressWarnings("unchecked") List<Person> findAll(); @SuppressWarnings("unchecked") List<Person> findByLastname(String name); void save(Person person); Person find(Long id); }
HibernatePersonDao implements PersonDao { @SuppressWarnings("unchecked") public List<Person> findByLastname(String name) { try { Session session = sessionFactory.getCurrentSession(); String hql = "from Person p where p.lastname = :lastname"; Query query = session.createQuery(hql); query.setParameter("lastname", name); return query.list(); } catch (HibernateException e) { throw new RuntimeException(e); } }  void setSessionFactory(SessionFactory sessionFactory); @SuppressWarnings("unchecked") List<Person> findAll(); @SuppressWarnings("unchecked") List<Person> findByLastname(String name); void save(Person person); Person find(Long id);  }