简体   繁体   English

如何在Hibernate / JPA中使用DAO?

[英]How to use DAOs with hibernate/jpa?

Assuming the DAO structure and component interaction described below, how should DAOs be used with persistence layers like hibernate and toplink? 假设下面描述的DAO结构和组件交互,如何将DAO与持久层(例如休眠和顶部链接)一起使用? What methods should/shouldn't they contain? 它们应该/不应该包含哪些方法?

Would it be bad practice to move the code from the DAO directly to the service? 将代码直接从DAO移到服务中是否是不好的做法?

For example, let's say that for every model we have a DAO (that may or may not implement a base interface) that looks something like the following: 例如,假设对于每个模型,我们都有一个DAO(可能实现也可能不实现基本接口),其外观类似于以下内容:

public class BasicDao<T> {
 public List<T> list() { ... }
 public <T> retrieve() { ... }
 public void save() { ... }
 public void delete() { ... }
}

Component interaction pattern is -- 组件交互模式为-

service > DAO > model 服务> DAO>模型

We found (as have others) that it was straightforward to write a generic DAO as a thin shim on top of the JPA calls. 我们发现(和其他人一样)很容易在JPA调用之上编写通用DAO作为薄垫片。

Many were simply right on top of the JPA. 许多人只是在JPA之上。 For example, early on we simply had: 例如,一开始我们只是拥有:

public <T> T update(T object) {
    return em.merge(object);
}

But the benefit of having the layer is that your layer is extensible, whereas the EM is not. 但是拥有该层的好处是您的层是可扩展的,而EM则不是。

Later we added an overload: 后来我们添加了一个重载:

public Thing update(Thing object) {
    // do magic thing processing
}

So, our layer was basically intact, but could handle custom processing. 因此,我们的图层基本上是完整的,但是可以处理自定义处理。

For example, later, since early JPA didn't have Orphan processing, we added that in our backend service. 例如,稍后,由于早期的JPA没有进行孤儿处理,因此我们将其添加到了后端服务中。

Even simple common DAO has value, simply as an abstraction point. 甚至简单的普通DAO都具有价值,仅仅是作为抽象点。

We just didn't need to make one for every group of objects (CustomerDAO, OrderDAO, etc.) like the olden days. 我们只是不需要像过去那样为每组对象(CustomerDAO,OrderDAO等)制作一个。

IMHO there is no method the DAO "should" contain in general. 恕我直言,DAO通常不应该包含任何方法。 It should contain exactly those methods your application needs. 它应确切包含您的应用程序需要的那些方法。 This may differ from model to model. 这可能因型号而异。

In Hibernate and JPA, methods like save and retrieve are "trivial" operations provided by the session / entity manager, so I don't see much point in adding them to the DAO, apart from maybe insulating the service / business logic from the actual persistence implementation. 在Hibernate和JPA中,诸如saveretrieve类的方法是会话/实体管理器提供的“琐碎”操作,因此,除了将服务/业务逻辑与实际隔离之外,我看不出将它们添加到DAO的意义。持久性实现。 However, JPA is already an insulation in itself. 但是,JPA本身已经是隔离。

Moving persistence code directly into the service layer would bundle the two together. 将持久性代码直接移入服务层会将两者捆绑在一起。 In a small app this might be OK, but over time even small apps tend to grow, and maintenance becomes an issue. 在小型应用程序中,这也许可以,但是随着时间的流逝,即使小型应用程序也趋于增长,维护成为一个问题。 Keeping separate concerns separated helps keep the code clean, thus easier to understand, extend and reuse. 将单独的关注点分开可以帮助保持代码干净,从而更易于理解,扩展和重用。

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

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