Spring – JPA and Dao – what is the standard approach?

I am developing my first application using JPA / Hibernate and spring My first attempt in Dao class looked 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 of using jpadaosupport and jpatemplate Which design do you prefer? What's wrong with my example?

Solution

I'd say your method sounds perfect Personally, I don't use jpadaosupport or jpatemplate because you can use entitymanager and criteria queries to do everything you need

Quoted from Javadoc of jpatemplate:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>