Spring DaoSupport和@PersistanceContext EntityManager? [英] Spring DaoSupport and @PersistanceContext EntityManager?

查看:88
本文介绍了Spring DaoSupport和@PersistanceContext EntityManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解Spring最困难的事情之一是Spring支持针对同一问题的多种方法.

One of the most difficult things about understand Spring is that Spring supports multiple approaches to the same problem.

因此,在我的应用程序中,我通过@PersistanceContext批注使用注入的EntityManager,例如:

So in my application I using injected EntityManager using the @PersistanceContext annotation, for example:

@Repository
public class JpaDao extends JpaDaoSupport implements Dao {

    @PersistenceContext(unitName = "PersistanceUnit", type = PersistenceContextType.EXTENDED)
    private EntityManager em;

此方法是否与扩展JpaDaoSupport (需要在EntityManager中注入)兼容?对我来说,这似乎是解决同一问题的两种不兼容的方法,但是我想从对Spring有更多经验的人那里获得一些建议.

Is this approach compatible with extending JpaDaoSupport (which requires injecting in an EntityManager)? To me it looks like two incompatible approaches to the solving the same problem, but I would like some advice from someone who has more experience with Spring.

如果我不应该扩展JpaDaoSupport,应该如何使用@PersistenceContext方法构建DAO?

If I shouldn't be extending JpaDaoSupport, how should I build my DAO using the @PersistenceContext approach?

推荐答案

您正确的说,它们是解决同一问题的两种不同方法.我认为哪一个更好"是一个品味问题.使用注释的好处是避免了代码中的Spring导入依赖关系,甚至 JpaDaoSupport 的Spring JavaDoc都建议将其用于新的JPA项目. JpaDaoSupport 可以使Spring对JPA的支持与对其他ORM策略( HibernateDaoSupport JdbcDaoSupport TopLinkDaoSupport >等).在这些情况下,基于注释的注入是不可行的.

You're correct that they are two different approaches to the same problem. Which one is "better" is a matter of taste, I think. Using annotations has the benefit of avoiding Spring import dependencies in your code, and even the Spring JavaDoc for JpaDaoSupport suggests using them for new JPA projects. JpaDaoSupport is there to make Spring's support for JPA equivalent to its support for other ORM strategies (HibernateDaoSupport, JdbcDaoSupport, TopLinkDaoSupport, etc.). In those cases, annotation-based injection isn't an option.

这篇关于Spring DaoSupport和@PersistanceContext EntityManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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