2015-10-05 39 views
1

我將Hibernate 4替換爲Hibernate 5,現在面臨着連接元數據的問題。代碼片段:Hibernate 5:如何將連接的元數據轉換爲DialectresolutionInfo

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爲我工作沒有演員。

謝謝。

+0

問題解決autowirin g SessionFactory並從中取出Dialect。 – maksim2020

回答

0

我不得不升級到休眠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())); 
相關問題