You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Intro

The Intro page provides an overview, the setup of this module and describes the motivation for the features described below. This page explains the most important APIs and mechanisms of the JPA module provided by CODI. Please note that this page doesn't show all possibilities. If you have any question, please contact the community!

Using one (default) Entity Manager

Producer for a default entity manager
public class DataBaseProducer
{
    @Produces
    @PersistenceContext(unitName="default")
    @Default
    private EntityManager entityManager;

    public void dispose(@Disposes @Default EntityManager entityManager)
    {
        if(entityManager.isOpen())
        {
            entityManager.close();
        }
    }
}

Hint

For using @PersistenceContext you can use e.g. the resources-plugin of OpenWebBeans

Using @Transactional
public class CustomService1
{
    @Inject
    protected EntityManager entityManager;

    @Transactional
    public void update(CustomEntity1 entity)
    {
        this.entityManager.merge(entity);
    }
}

Using multiple Entity Managers

Producer for entity managers
public class DataBaseProducer
{
    @Produces
    @PersistenceContext(unitName="default")
    @Default
    private EntityManager entityManager;

    @Produces
    @PersistenceContext(unitName="UserDB")
    @Users
    private EntityManager usersEntityManager;

    public void disposeDefaultDB(@Disposes @Default EntityManager entityManager)
    {
        if(entityManager.isOpen())
        {
            entityManager.close();
        }
    }

    public void disposeUserDB(@Disposes @Users EntityManager entityManager)
    {
        if(entityManager.isOpen())
        {
            entityManager.close();
        }
    }
}
Using @Transactional and an entity manager with a qualifier
public class CustomService2
{
    @Inject
    @Users
    protected EntityManager entityManager;

    @Transactional(Users.class)
    public void update(CustomEntity2 entity)
    {
        this.entityManager.merge(entity);
    }
}
  • No labels