【问题标题】:Executing native query with Hibernate 4.1使用 Hibernate 4.1 执行本机查询
【发布时间】:2012-07-17 14:41:03
【问题描述】:

我正在使用带有 C3P0 连接池的 Hibernate。在 Hibernate 3 中,我可以通过 BorrowedConnectionProxy 访问包装的 C3P0ProxyConnection,然后执行 rawConnectionOperation。据我所知,BorrowedConnectionProxy 不再是 Hibernate 4.1 的一部分。

有什么方法可以运行供应商特定的查询吗? (Work.execute 中的代理连接实例对我不起作用,我需要执行 Oracle 存储过程来收集自定义对象类型)。

谢谢。

【问题讨论】:

    标签: oracle hibernate


    【解决方案1】:

    您可以通过以下方式访问未代理的工作连接:

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

    该表单利用 JDBC 4 unwrap 方法,我们只需将其委托给底层连接。或者,如果您特别需要 OracleConnection:

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

    你也可以使用:

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

    我在考虑允许作品表示它想要一个未经代理的连接方面来回走动,但鉴于 Connection#unwrap 的可用性,我不太确定是否有真正的好处。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多