I’m developing my first app with JPA/Hibernate and Spring. My first attempt at a DAO class looks like this:
@Repository(value = "userDao")
public class UserDaoJpa implements UserDao {
@PersistenceContext
private EntityManager em;
public User getUser(Long id) {
return em.find(User.class, id);
}
public List getUsers() {
Query query = em.createQuery("select e from User e");
return query.getResultList();
}
}
I also found some examples using JpaDaoSupport and JpaTemplate. Which design do you prefer? Is there anything wrong with my example?
I’d say your approach looks totally sound. Personally I don’t use
JpaDaoSupportorJpaTemplatebecause you can do everything you need with theEntityManagerand Criteria Queries.Quote from the JavaDoc of JpaTemplate: