Java 单一 DAO 和通用 CRUD 方法(JPA/Hibernate + Spring)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3888575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Single DAO & generic CRUD methods (JPA/Hibernate + Spring)
提问by John Manak
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. The use of generic CRUD methods was proposed, but I'm not very sure how to implement this using JPA. Could you please give me an example or share a link regarding this?
继我之前的问题DAO 和服务层 (JPA/Hibernate + Spring) 之后,我决定在使用 JPA/Hibernate、Spring 和 Wicket 的应用程序中为我的数据层(至少在开始时)只使用一个 DAO。建议使用通用 CRUD 方法,但我不太确定如何使用 JPA 实现它。你能给我一个例子或分享一个关于这个的链接吗?
采纳答案by Pascal Thivent
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);
}
}
回答by altuure
if you are looking for a third party implementation , you can check http://www.altuure.com/projects/yagdao/. it is a nnotation based generic DAO framework which supports JPA and hibernate
如果您正在寻找第三方实现,您可以查看 http://www.altuure.com/projects/yagdao/。它是一个基于注解的通用 DAO 框架,支持 JPA 和 hibernate
回答by Danny C
I was looking for this same thing. I found what appears to be exactly that- the Spring-DataJPA project provided by SpringSource. This is a code port from Hadesand has now (Early 2011) been swallowed by Spring and better integrated.
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. Also allows grails like queries just by using params names as the name of the method- in the interface (no implementation required!) i.e. findByLastname(String lastName);
Looks very promising- being part of Spring projects will certainly ensure some future for it too.
I have begun implementing this in my upcoming project now.
我正在寻找同样的东西。我找到了似乎正是SpringSource 提供的Spring-DataJPA 项目。这是来自Hades的代码移植,现在(2011 年初)被 Spring 吞并并更好地集成。它允许您使用带有静态创建的单个 dao (SimpleJpaRepository),或扩展基础 JpaRepository 类以使用现成的 CRUD+ 方法创建任何特定于对象的 dao。还允许使用 params 名称作为方法名称的 grails 查询 - 在接口中(不需要实现!)即findByLastname(String lastName);
看起来非常有前途 - 作为 Spring 项目的一部分肯定也会确保它的未来。我现在已经开始在我即将到来的项目中实现这一点。
回答by Balazs Zsoldos
Based on the article Don't repeat the DAOwe used this kind of technique for many years. We always struggled with problems with our patterns after we realized that we made a big mistake.
基于文章不要重复我们使用这种技术多年的 DAO。在我们意识到我们犯了一个大错误后,我们总是与我们的模式问题作斗争。
By using an ORM tool such as Hibernate or JPA you will not have to think DAO and Service layer separately. You can use EntityManager from your service classes as you know the lifecycle of transactions and the logic of your entity classes there.
通过使用 Hibernate 或 JPA 等 ORM 工具,您将不必分别考虑 DAO 和服务层。您可以从服务类中使用 EntityManager,因为您知道事务的生命周期和实体类的逻辑。
Do you save any minute if you call myDao.saveEntity
instead of simply entityManager.saveEntity
? No. You will have an unnecessary dao class that does nothing else but will be a wrapper around EntityManager. Do not afraid to write selects in your service classes with the help of EntityManager (or session in hibernate).
如果你打电话myDao.saveEntity
而不是简单地打电话,你会节省任何时间entityManager.saveEntity
吗?不。你将有一个不必要的 dao 类,它什么都不做,但将是 EntityManager 的包装器。不要害怕在 EntityManager(或休眠中的会话)的帮助下在您的服务类中编写选择。
One more note: You should define the borders of your service layer and do not let programmers to return or wait for Entity classes. The UI or WS layer programmers should not know at all about entity classes only about 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 或 WS 层程序员不应该只了解 DTO-s 的实体类。实体对象具有大多数程序员不知道的生命周期。如果将实体对象存储在会话数据中并尝试在几秒钟或几小时后将其更新回数据库,则会遇到非常严重的问题。好吧,您可能不会这样做,但是知道服务层的参数类型和返回类型的 UI 程序员只会为了节省一些代码行而这样做。
回答by bennidi
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 的配置示例集成