1

我确实将 Hibernate 4 替换为 Hibernate 5,现在面临连接元数据的问题。代码片段是:

public long getNext(final String sequenceName) {
    ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
        @Override
        public Long execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            >>>>> problem is here >>>>>> Dialect dialect =  dialectResolver.resolveDialect((DialectResolutionInfo)connection.getMetaData());
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement( dialect.getSequenceNextValString(sequenceName));
                resultSet = preparedStatement.executeQuery();
                resultSet.next();
                return resultSet.getLong(1);
            }catch (SQLException e) {
                throw e;
            } finally {
                if(preparedStatement != null) {
                    preparedStatement.close();
                }
                if(resultSet != null) {
                    resultSet.close();
                }
            }

        }
    };
    Long maxRecord = databaseUtilities.getSession().doReturningWork(maxReturningWork);
    return maxRecord;
}

这段代码可以编译,但给了我一个 java.lang.ClassCastException:com.mchange.v2.c3p0.impl.NewProxyDatabaseMetaData cannot be cast to org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo。

有没有正确的方法来做到这一点?Hibernate 4 在没有那个演员表的情况下为我工作。

谢谢你。

4

1 回答 1

0

升级到 Hibernate 5 时我遇到了类似的问题。特别是我的堆栈跟踪会显示:

org.apache.tomcat.dbcp.dbcp2.DelegatingDatabaseMetaData 
cannot be cast to org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo

您可以通过导入以下适配器轻松执行该转换:

import org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter;

并将您标记为有问题的行替换为以下行:

Dialect dialect =  dialectResolver.resolveDialect(
               new DatabaseMetaDataDialectResolutionInfoAdapter(connection.getMetaData()));
于 2016-01-28T16:02:03.687 回答