EntityManagerFactory + jpadaosupport的依赖项注入问题 [英] Dependency Injection problem with EntityManagerFactory + jpadaosupport

查看:468
本文介绍了EntityManagerFactory + jpadaosupport的依赖项注入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在将EntityFactoryManager注入到我的jpadaosupport扩展类中时遇到问题.

Right now I'm having a problem injecting a entityFactoryManager into my jpadaosupport extended class.

我的配置如下:

<bean id="productDao" class="springapp.repository.JdbcProductDao">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

此bean的上述配置可以正常工作,但是当我尝试使用批注来配置bean时,我的应用程序将无法正常工作

The above configuration for this bean works fine however when I try to use annotations to configure the bean my application doesn't work

我的JdbcProductDao.java文件在下面

My JdbcProductDao.java file is below

@Repository("productDao")
@Transactional
public class JdbcProductDao extends JpaDaoSupport implements ProductDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Product> getProductList() {
        // TODO Auto-generated method stub
        return getJpaTemplate().getEntityManagerFactory().createEntityManager()
                .createQuery("from Product").getResultList();
    }

    @Override
    public void persist(Product product) {
        // TODO Auto-generated method stub

    }

    @Override
    public void saveProduct(Product prod) {
        // TODO Auto-generated method stub
        getJpaTemplate().merge(prod);
    }

    @Autowired
    @Required
    public void setJpaEntityManagerFactory(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        super.setEntityManagerFactory(entityManagerFactory);
    }
}

但是似乎EntityManagerFactory似乎没有被正确注入,因为没有看到我的数据库事务

However it seems as though the EntityManagerFactory is not injected properly because none of my database transactions are seen

有人可以提供任何见识吗?

Could anybody offer any insight?

推荐答案

遵循

After following Pascal's tips, you will probably resolve your problem.

不过,根据您的代码,我还有其他建议:

However I have another advice based on your code:

  • 如果要使用JpaDaoSupport,请使用JpaTemplate方法.
  • 如果要直接使用EntityManager,则通过@PersistenceContext注入它,不要使用JpaDaoSupport
  • if you are going to use JpaDaoSupport, use the JpaTemplate methods.
  • if you want to use EntityManager directly, then inject it via @PersistenceContext, and don't use JpaDaoSupport

调用createEntityManager()可能会中断spring的事务处理.这是因为事务管理器创建了当前的EntityManager,以后将使用它.如果您自己创建它,则实际上可能会遇到两个不同的EntityManager-一个正在进行的交易,另一个-没有.

Calling createEntityManager() may disrupt the transaction handling of spring. This is because the transaction manager creates the current EntityManager which is later used. If you create it yourself, you may effectively end up having 2 different EntityManagers - one with an ongoing transaction, and one - not.

这篇关于EntityManagerFactory + jpadaosupport的依赖项注入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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