0

I tried create BaseDao and inject EntityManager to it. In Spring I was make this:

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

    @PersistenceContext(unitName = "access")
    protected EntityManager entityManager;

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

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

but now I tried make this in OSGI and I not understand how do it. I treid write in blueprint.xml

<bean id="baseJpaDao" class="domain.access.impl.BaseJpaDao" >
    <jpa:context unitname="access" property="entityManager"/>
    <tx:transaction method="*" value="RequiresNew"/>
</bean>

and after this

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

    private EntityManager entityManager;

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

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

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

I treid like this link

but not help.

I tried this

EntityManagerFactory emf = Persistence.createEntityManagerFactory("access", System.getProperties());
        em = emf.createEntityManager();

but not help.

Community
  • 1
  • 1
user5620472
  • 2,722
  • 8
  • 44
  • 97

1 Answers1

1

In Aries JPA 1.x that does not work.

In Aries JPA 2.x you can use exactly the same code as in spring. See TaskServiceImpl.java. You just need to add the jpa:enable and jta:enable elements to you blueprint context to activate the functionality.

Alternatively you can use the blueprint-maven-plugin to generate the whole blueprint.xml from annotations. Btw version 1.3.0 should be fine. You do not need the snapshot.

Thanks for the configuration snippets. The problem is that you use the wrong namespace for jpa. Try these:

xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0"
Christian Schneider
  • 19,420
  • 2
  • 39
  • 64