gpt4 book ai didi

java - hibernate 5 : how to cast connection's metadata to DialectresolutionInfo

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:26 24 4
gpt4 key购买 nike

我确实将 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无法转换为org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo。

有正确的方法吗? Hibernate 4 在没有那个 Actor 的情况下也能为我工作。

谢谢。

最佳答案

升级到 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()));

关于java - hibernate 5 : how to cast connection's metadata to DialectresolutionInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32956500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com