Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get persistence unit name from EntityManager

Tags:

jpa

jakarta-ee

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.

like image 942
JBT Avatar asked Mar 22 '23 17:03

JBT


1 Answers

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()
like image 199
MBBA Avatar answered Mar 31 '23 13:03

MBBA