4

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

What is another method,please,instead of this

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Pramil K Prince
  • 113
  • 3
  • 12
  • this question is pretty thoroughly discussed here. http://stackoverflow.com/questions/8770459/connection-getmetadata-does-not-seem-to-return-table-info – Nozone Aug 06 '13 at 16:10

3 Answers3

2

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?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Last line of your post was enough to add as a comment to question, rather than posting a new answer. Nothing new other than API link. – Nayan Wadekar Mar 05 '13 at 08:47
  • @NayanWadekar Thanks for the concern. If you see that link there are 4 to 5 ways of getting connection object. But the one I written above looks better among those(As per my understanding after read all the possibilities there). – Suresh Atta Mar 05 '13 at 08:57
  • I think, a small green tick & the number of votes are enough to minimize the possibilities. – Nayan Wadekar Mar 05 '13 at 09:00
0

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.

Community
  • 1
  • 1
luca.vercelli
  • 898
  • 7
  • 24
0

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()
    }
}