简体   繁体   English

单DAO和通用CRUD方法(JPA / Hibernate + Spring)

[英]Single DAO & generic CRUD methods (JPA/Hibernate + Spring)

Following my previous question, DAO and Service layers (JPA/Hibernate + Spring) , I decided to use just a single DAO for my data layer (at least at the beginning) in an application using JPA/Hibernate, Spring and Wicket. 在我之前的问题, DAO和服务层(JPA / Hibernate + Spring)之后 ,我决定在使用JPA / Hibernate,Spring和Wicket的应用程序中仅使用一个DAO作为我的数据层(至少在开始时)。 The use of generic CRUD methods was proposed, but I'm not very sure how to implement this using JPA. 提出了使用通用CRUD方法,但我不太确定如何使用JPA实现它。 Could you please give me an example or share a link regarding this? 你能给我一个例子或分享一个关于这个的链接吗?

Here is an example interface: 这是一个示例界面:

public interface GenericDao<T, PK extends Serializable> {
    T create(T t);
    T read(PK id);
    T update(T t);
    void delete(T t);
}

And an implementation: 并实施:

public class GenericDaoJpaImpl<T, PK extends Serializable> 
    implements GenericDao<T, PK> {

    protected Class<T> entityClass;

    @PersistenceContext
    protected EntityManager entityManager;

    public GenericDaoJpaImpl() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass()
             .getGenericSuperclass();
        this.entityClass = (Class<T>) genericSuperclass
             .getActualTypeArguments()[0];
    }

    @Override
    public T create(T t) {
        this.entityManager.persist(t);
        return t;
    }

    @Override
    public T read(PK id) {
        return this.entityManager.find(entityClass, id);
    }

    @Override
    public T update(T t) {
        return this.entityManager.merge(t);
    }

    @Override
    public void delete(T t) {
        t = this.entityManager.merge(t);
        this.entityManager.remove(t);
    }
}

Based on the article Don't repeat the DAO we used this kind of technique for many years. 基于文章不要重复DAO,我们多年来一直使用这种技术。 We always struggled with problems with our patterns after we realized that we made a big mistake. 在我们意识到我们犯了一个大错误之后,我们总是在解决模式问题。

By using an ORM tool such as Hibernate or JPA you will not have to think DAO and Service layer separately. 通过使用诸如Hibernate或JPA之类的ORM工具,您不必分别考虑DAO和服务层。 You can use EntityManager from your service classes as you know the lifecycle of transactions and the logic of your entity classes there. 您可以使用服务类中的EntityManager,因为您知道事务的生命周期和实体类的逻辑。

Do you save any minute if you call myDao.saveEntity instead of simply entityManager.saveEntity ? 如果你调用myDao.saveEntity而不仅仅是entityManager.saveEntity你会保存吗? No. You will have an unnecessary dao class that does nothing else but will be a wrapper around EntityManager. 不会。你将有一个不必要的dao类,除了将成为EntityManager的包装器之外什么也不做。 Do not afraid to write selects in your service classes with the help of EntityManager (or session in hibernate). 不要害怕在EntityManager(或hibernate中的会话)的帮助下在服务类中编写选择。

One more note: You should define the borders of your service layer and do not let programmers to return or wait for Entity classes. 还有一点需要注意:您应该定义服务层的边框,不要让程序员返回或等待Entity类。 The UI or WS layer programmers should not know at all about entity classes only about DTO-s. UI或WS层程序员根本不应该只知道有关DTO-s的实体类。 Entity objects have lifecycles that most of the programmers do not know about. 实体对象具有大多数程序员不了解的生命周期。 You will have really serious issues if you store an entity object in a session data and try to update it back to the database seconds or hours later. 如果将实体对象存储在会话数据中并尝试在几秒或几小时后将其更新回数据库,则会出现严重问题。 Well you may would not do it but a programmer of the UI who knows the parameter types and return types of your service layer only would do to save some lines of code. 好吧,你可能不会这样做,但UI的程序员只知道服务层的参数类型和返回类型,只能保存一些代码行。

I was looking for this same thing. 我在寻找同样的事情。 I found what appears to be exactly that- the Spring-Data JPA project provided by SpringSource. 我发现了SpringSource提供的Spring-Data JPA项目。 This is a code port from Hades and has now (Early 2011) been swallowed by Spring and better integrated. 这是来自Hades的代码端口,现在(2011年初)被Spring吞并并更好地集成。 It allows you to use a single dao (SimpleJpaRepository) with a static create, or extend the base JpaRepository class to create any object specific dao with ready made CRUD+ methods. 它允许您将单个dao(SimpleJpaRepository)与静态create一起使用,或者扩展基本JpaRepository类以使用现成的CRUD +方法创建任何特定于对象的dao。 Also allows grails like queries just by using params names as the name of the method- in the interface (no implementation required!) ie findByLastname(String lastName); 也可以通过使用params名称作为接口中的方法名称(不需要实现!),即findByLastname(String lastName); Looks very promising- being part of Spring projects will certainly ensure some future for it too. 看起来非常有前途 - 作为Spring项目的一部分肯定会确保它的未来。 I have begun implementing this in my upcoming project now. 我现在已经开始在即将开展的项目中实现这一点。

if you are looking for a third party implementation , you can check http://www.altuure.com/projects/yagdao/ . 如果您正在寻找第三方实施,可以查看http://www.altuure.com/projects/yagdao/ it is a nnotation based generic DAO framework which supports JPA and hibernate 它是一个基于注释的通用DAO框架,支持JPA和hibernate

You may also have a look at http://codeblock.engio.net/data-persistence-and-the-dao-pattern/ 您也可以查看http://codeblock.engio.net/data-persistence-and-the-dao-pattern/

The related code can be found on github https://github.com/bennidi/daoism 相关代码可以在github https://github.com/bennidi/daoism上找到

It has integration with Spring and configuration examples for Hibernate and EclipseLink 它集成了Spring和Hibernate和EclipseLink的配置示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM