7

Is it possible to get the associated persistence unit name of an EntityManager object? For example, you have

@PersistenceContext( unitName="fooPU" )
private EntityManager em;

Is it possible to get the name fooPU from em? The motivation for this is that I want to have a small test to verify that the injected em through @Inject is associated with the right persistence unit.

4
  • 1
    From the top of my head, the properties of the EMFactory (entityManager.getEntityManagerFactory().getProperties()) might contain the PU name
    – kostja
    Nov 9, 2013 at 7:46
  • 1
    @kostja: Thanks. As a matter of fact, that is my current workaround, except that I don't have the getEntityManagerFactory(), i.e., I use entityManager.getProperties(), which gives the same property map as yours. But that map doesn't have a standalone PU name key. It does have the PU name embedded in some other keys, though. You can get the PU name with some hacking, but it just doesn't seem clean to me.
    – JBT
    Nov 9, 2013 at 16:08
  • Yeah, clean would be different :) Unfortunately, I am not aware of a different way.
    – kostja
    Nov 10, 2013 at 8:13
  • 2
    I know it's a bit old post but you can consider asking JPA EG about providing such feature. If it's easy to add - why not try asking for it. If it's not possible because of some reasons it would also be nice to know why. Apr 22, 2014 at 10:48

1 Answer 1

8

the persistance unit name is under the key "hibernate.ejb.persistenceUnitName" in the properties Map

String puName = em.getEntityManagerFactory().getProperties().get("hibernate.ejb.persistenceUnitName").toString()
1
  • 8
    What if the JPA provider is not hibernate, like EclipseLink?
    – JBT
    Aug 7, 2014 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.