【问题标题】:How can I get entityManager in BaseDao in Karaf 4.0.5?如何在 Karaf 4.0.5 的 BaseDao 中获取 entityManager?
【发布时间】:2016-04-29 06:46:18
【问题描述】:

我使用karaf 4.0.5 和休眠4.2.15 我想在我的BaseDao 课程中获得EntityManager。 如果我尝试在我的服务中获取 EntityManager

<bean id="subscriberService" class="domain.payment.impl.subscriber.SubscriberServiceImpl" scope="singleton"
          init-method="init">
        <tx:transaction method="*" />
    </bean>

在课堂上

@PersistenceContext(unitName="payment")
    private EntityManager entityManager;

我得到 EntityManager 正常。

但如果我在另一个班级尝试过

public class BaseJpaDao<E> implements BaseDao<E>{
    protected Class<?> entityClass;

    @PersistenceContext(unitName="payment")
    private EntityManager entityManager;

    public BaseJpaDao(Class<?> entityClass) {
        this.entityClass = entityClass;
    }

    @Override
    public E persist(E e) {
        entityManager.persist(e);
        return e;
    }

我的entityManager 为空;

我试过了

<bean id="baseDao" class="domain.payment.impl.base.BaseJpaDao" scope="singleton"
          init-method="init">
        <tx:transaction method="*" />
    </bean>

但这无济于事。

在 Spring 项目中它工作正常,但在 OSGi 中我有很多问题。

真的只有通过服务我才能得到entityManager

【问题讨论】:

    标签: java jpa-2.0 entitymanager apache-karaf


    【解决方案1】:

    你检查过日志吗? BaseJpaDao 似乎没有公共的空构造函数,因此 karaf.log 中应该有一个错误,指出无法创建 baseDaobean...

    【讨论】:

    • 不确定这是否是问题,但值得尝试创建一个空的构造函数,看看它是否可以工作。
    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 2011-08-08
    • 2013-01-19
    • 1970-01-01
    • 2017-11-03
    • 2014-04-10
    • 2015-04-28
    • 2016-06-17
    相关资源
    最近更新 更多