a1ex07
a1ex07

Reputation: 37354

Executing native query with Hibernate 4.1

I'm using Hibernate with C3P0 connection pool. In Hibernate 3 I could get access to wrapped C3P0ProxyConnection through BorrowedConnectionProxy and then perform rawConnectionOperation. From what I see BorrowedConnectionProxy is not a part of Hibernate 4.1 anymore.

Is it any way I can run vendor specific queries ? (An instance of proxy connection inside Work.execute does not work for me, I need to execute Oracle stored procedure that takes collection of custom object type).

Thank you .

Upvotes: 2

Views: 1228

Answers (1)

Steve Ebersole
Steve Ebersole

Reputation: 9443

You can get access to the unproxied Connection in Work by calling:

public void execute(Connection connection) throws SQLException {
    Connection unproxiedConnection = connection.unwrap( Connection.class );
    ...
}

That form leverages the JDBC 4 unwrap method, we simply delegate that to the underlying connection. Or if you specifically need an OracleConnection:

public void execute(Connection connection) throws SQLException {
    OracleConnection oracleConnection = connection.unwrap( OracleConnection.class );
    ...
}

You could also use:

public void execute(Connection connection) throws SQLException {
    Connection unproxiedConnection = ( (JdbcWrapper<Connection>) connection ).getWrappedObject();
    ...
}

I have gone back and forth in terms of contemplating allowing the Work to signify that it wants an unproxied Connection, but given the availability of Connection#unwrap I am not so sure there is an real benefit.

Upvotes: 4

Related Questions