微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

春天 – JPA和DAO – 什么是标准方法?

我正在使用JPA / Hibernate和 Spring开发我的第一个应用程序.我在DAO课上的第一次尝试看起来像这样:
@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();
    }
}

我还发现了一些使用JpaDaoSupport和JpaTemplate的例子.你更喜欢哪种设计?我的例子有什么问题吗?

解决方法

我会说你的方法听起来很完美.我个人不使用JpaDaoSupport或JpaTemplate,因为您可以使用EntityManager和Criteria Queries完成所需的一切.

引自JavaDoc of JpaTemplate

JpaTemplate mainly exists as a sibling of JdoTemplate and HibernateTemplate,offering the same style for people used to it. For newly started projects,consider adopting the standard JPA style of coding data access objects instead,based on a “shared EntityManager” reference injected via a Spring bean deFinition or the JPA PersistenceContext annotation.

原文地址:https://www.jb51.cc/java/125664.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐