Get Connection object while using SessionFactory.getCurrentSession() in Hibernate
<p>I'm trying to get <code>Connection</code> object in Hibernate when <code>SessionFactory.getCurrentSession()</code> is used.</p>

<p><strong>Source code</strong></p>

<pre><code>import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.internal.SessionImpl;

public class SOExample {
    public static void main(String[] args) throws SQLException {
        Configuration configuration = new Configuration();
        SessionFactory  sessionFactory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());
        Session session = sessionFactory.getCurrentSession();
        Connection connection = ((SessionImpl) session).connection();
        // doing operation on connection object as per my requirement
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        System.out.println(databaseMetaData.getDatabaseProductName());
    }
}
</code></pre>

<p><strong>Stacktrace</strong></p>

<pre><code>Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy24 cannot be cast to org.hibernate.internal.SessionImpl
    at com.SOExample.main(SOExample.java:20)
</code></pre>

<p><code>getCurrentSession()</code> gives <code>Proxy</code> object of <code>Session</code> so It can't cast it to <code>SessionImpl</code> so what are the other ways to get <code>Connection</code> object. Or How to <code>SessionImpl</code> from Proxy object.</p>

<hr>

<p>Other option I tried but it says <code>getConnectionProvider()</code> method not found.</p>

<pre><code>SessionFactoryImplementor sessionFactoryImplementation = (SessionFactoryImplementor) session.getSessionFactory();
ConnectionProvider connectionProvider = sessionFactoryImplementation.getConnectionProvider();
try {
    Connection connection = connectionProvider.getConnection();
} catch (SQLException e) {
    e.printStackTrace();
}
</code></pre>

<p><strong>Note</strong>: I'm using <strong>hibernate-core-5.0.5.Final.jar</strong></p>