Pramil K Prince
Pramil K Prince

Reputation: 113

Alternative of deprecated hibernate getSession().connection()

When using HibernateDaoSupport class,I found that getSession().connection(); is deprecated.

What is another method,please,instead of this

Upvotes: 4

Views: 8693

Answers (3)

bzarani
bzarani

Reputation: 1

You can use org.hibernate.internal.SessionImpl;

import org.hibernate.internal.SessionImpl;
public static class WorkAround {
    public Connection getConnection(Session session){
            SessionImpl sessionImpl = (SessionImpl) session;
            return session.connection()
    }
}

Upvotes: 0

luca.vercelli
luca.vercelli

Reputation: 1038

I have found the following workaround for getting Session's connection without any deprecation. I'm pretty sure Hibernate's people didn't meant to allow this.

private static class WorkAround implements org.hibernate.jdbc.Work {
    private Connection conn = null;

    @Override
    public void execute(Connection conn) throws SQLException {
        this.conn = conn;
    }

    public Connection getConnection() {
        return conn;
    }

}

public static Connection getConnection(Session session) {
    WorkAround wrk = new WorkAround();
    session.doWork(wrk);
    return wrk.getConnection();
}

I wanted to post this answer here: session.connection() deprecated on Hibernate?, but I see I'm not allowed to.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Now we have to use session.doWork() API:

session.doWork(
        new Work() {
            public void execute(Connection connection) throws SQLException 
            { 
                doSomething(connection); 
            }
        }
    );

Refer also :session.connection() deprecated on Hibernate?

Upvotes: 2

Related Questions