使用jpa2/eclipselink合适的DAO结构是什么? [英] What's an appropriate DAO structure with jpa2/eclipselink?

查看:109
本文介绍了使用jpa2/eclipselink合适的DAO结构是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JPA实体,需要与它们执行逻辑.到目前为止,一个巨大的静态数据库类完成了这项工作.这很丑,因为每个公共接口方法都有一个使用EntityManager来执行事务的私有等效项.但是我也可以解决拥有静态em的问题! 但是我想知道这是否是一个合适的设计,尤其是因为该类负责许多事情. 毫不奇怪,我在网上找到的真实项目的代码并不容易理解(然后我可能还会对我的代码进行补救). 代码

I've JPA entities and need to perform logic with them. Until now a huge static database class did the job. It's ugly because every public interface method had an private equivalent that used the EntityManager, to perform transactions. But I could solve that having a static em too! However i'm wondering if that's an appropriate design, especially as the class is responsible for many things. Not surprisingly, the code i found online of real projects was not easy to understand (i might then as well remeain with my code). The code here is easy to understand, although maybe over generic? Anyway, on top of JDBC. Yet, insightful, why use factories and singletons for DAOs?

我已经按如下方式单调em实例:

I've though of singletoning the em instance as follows:

private static final Map<String, EntityManager> ems = new HashMap<String, EntityManager>();
private final EntityManager em;
private final EntityManagerFactory emf;

public void beginTransaction() {
    em.getTransaction().begin();
}

public void commitTransaction() {
    em.getTransaction().commit();
}

public Database(final String persistenceUnitName) {
    if(ems.containsKey(persistenceUnitName)){
        em = ems.get(persistenceUnitName);
    }else{
       ems.put(persistenceUnitName, em = Persistence.createEntityManagerFactory(persistenceUnitName).createEntityManager());
    }
    emf = em.getEntityManagerFactory();
    this.persistenceUnitName = persistenceUnitName;
}

这种创建实例的方式是标准的,但仍保持单例Connection/EntityManager. 另一方面,我想知道是否首先需要使用单例em? 优点是使用多个em时遇到了锁定问题(不使用em.lock()).

This way creation of instances is standard, still maintaining a singleton Connection/EntityManager. On the otherhand I wondered whether there was the need to singleton ems in the first place? The advantage is with multiple ems I run into locking problems (not using em.lock()).

有任何反馈意见吗?可以通过JPA2和eclipselink演示DAO的任何真实世界或教程代码吗?

Any feedback? Any real-world or tutorial code that demonstrates DAO with JPA2 and eclipselink?

推荐答案

我个人没有看到屏蔽EntityManager的附加值(这是 DAO ,除非有可能从JPA切换,否则我将直接从服务中使用它.但是,引用关于JPA的有趣辩论和DAO :

Personally, I don't see the added value of shielding the EntityManager (which is an implementation of the Domain Store pattern) with a DAO and I would use it directly from the services, unless switching from JPA is a likely event. But, quoting An interesting debate about JPA and the DAO:

亚当说,他只遇到过少数情况下的项目切换数据库供应商的案例,也没有遇到过持久性转移到与RDBM不同的案例的情况.您为什么要为不太可能发生的事情支付更多费用?有时,发生这种情况时,可能会得到一个更简单的解决方案,而它可能会为自己付出代价,并且可能会更简单地重写组件.

Adam said that he met only very few cases in which a project switched the database vendor, and no cases in which the persistence moved to a different thing than a RDBMs. Why should you pay more for a thing that it's unlikely to happen? Sometimes, when it happens, a simpler solution might have paid for itself and it might turn out to be simpler to rewrite a component.

我完全同意以上观点.

I totally share the above point of view.

无论如何,仍然需要解决的问题是EntityManager的生命周期,答案很大程度上取决于您的应用程序(Web应用程序,桌面应用程序)的性质.

Anyway, the question that remains open is the lifecycle of the EntityManager and the answer highly depends on the nature of your application (a web application, a desktop application).

以下一些链接可能有助于确定适合您的情况的

Here are some links that might help to decide what would be appropriate in your case:

  • Re: JPA DAO in Desktop Application
  • Using the Java Persistence API in Desktop Applications
  • Eclipselink in J2SE RCP Applications
  • Developing Applications Using EclipseLink JPA (ELUG)
  • An interesting debate about JPA and the DAO

如果您真的想采用DAO方式,则可以:

And if you really want to go the DAO way, you could:

  • use Spring JPA support,
  • use some generic DAO library like generic-dao, krank, DAO Fusion,
  • roll your own generic DAO.

这篇关于使用jpa2/eclipselink合适的DAO结构是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆