Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate cannot unwrap interface

I need help with

javax.persistence.PersistenceException: Hibernate cannot unwrap interface java.sql.Connection

I need to unwrap the connection. It's for a migration from glassfish to JBoss eap 7. I don't understand where the problem is. Does JBoss not accept this? Is there another way to do the unwrapping?

I read a lot about unwrapping the connection with Session, but my IDE says Session not found.

I work with JBoss eap 7 and hibernate 2.1.

like image 426
Illidan Avatar asked Mar 10 '23 03:03

Illidan


1 Answers

Jboss EAP7 supports Hibernate 5.x, not Hibernate 2.x. Use the Hibernate version bundled with specific JBoss EAP release.

entityManager.getTransaction().begin();

java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
entityManager.getTransaction().commit();

Session hibernateSession = entityManager.unwrap(Session.class);

hibernateSession.doWork(new org.hibernate.jdbc.Work() {

    @Override
    public void execute(Connection connection) throws SQLException {
        // do whatever you need to do with the connection
    }
});

see here: http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getting_a_JDBC_Connection_from_an_EntityManager

like image 89
Anup Dey Avatar answered Mar 21 '23 04:03

Anup Dey