tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <>
Subject Obtaining EntityManager programatically for test purposes
Date Tue, 03 Feb 2009 13:56:36 GMT
Hi @all,

as a lot of folks I am not really satisfied with how JBoss currently cares about their embedded
application server.
So I finally decided not to struggle around anymore, but to move to openEJB as this seems
a lot smoother and people speak very good about it.

So far everything works fine with openEJB, the only point I have is to obtain an EntityManager
programatically and not through dependency injection for test purposes.
I am trying to migrate the following function, which worked on JBoss:

public EntityManager getEntityManager(String persistenceUnitName) throws ResourceNotFoundException
{"getEntityManager " + persistenceUnitName);
    EntityManager em = null;

    String lookupString = "java:/EntityManagers/" + persistenceUnitName;

    try {
        LOG.trace(String.format("Looking for EntityManager at %s", lookupString));
        em = (EntityManager) lookup(lookupString);
    } catch (NamingException ex) {
        LOG.error(String.format("An error occured while looking for EntityManager at %s",
lookupString), ex);
        throw new ResourceNotFoundException("An error occured while looking for EntityManager
at %s", lookupString);

    LOG.trace(String.format("Returning EntityManager %s", em));
    return em;

So as you can see, I could lookup different entityManagers by their persistenceUnitName through
Is there anything similar, how this can be done using openEJB?

I saw solutions how the entityManager is obtained through a SessionBean after it has been
deployed and the EM has been injected.
But this can only be a work-around to me, as I see no chance with handling different entityManagers
with different persistence-units.

Anyone has an idea on this?


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message