...without actually reading and parsing the persistence.xml
I can retrieve the name of the persistence unit of an EntityManager using the p
I'm using Hibernate 5.0.x
This is how I'm getting a connection from the persistence pool:
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.jpa.internal.EntityManagerFactoryImpl;
public Connection getConnection(EntityManagerFactory emf) throws SQLException
{
final EntityManagerFactoryImpl hibernateEmf = (EntityManagerFactoryImpl) emf;
return hibernateEmf.getSessionFactory().getServiceRegistry().getService(ConnectionProvider.class).getConnection();
}
The emf parameter is JPA's standard javax.persistence.EntityManagerFactory, typically acquired globally using:
emf = Persistence.createEntityManagerFactory("persistence-unit-name");
or by injection:
@PersistenceUnit(unitName="persistence-unit-name")
EntityManagerFactory emf;