java - SQLiteJDBC 给 org.sqlite.MetaData.getImportedKeys not yet implemented error with Hibernate

标签 java hibernate sqlite jdbc sqlitejdbc

当与 hibernate 的“hbm2ddl.auto = update”设置一起使用时,SQLiteJDBC 给我以下异常:

org.sqlite.MetaData.getImportedKeys not yet implemented

有什么解决办法吗?我在下面找到了一个,并将其张贴在这里以供将来引用,但还有其他人有更好的想法吗?

最佳答案

通过一些浏览,我发现有人为它做了一个补丁,它是 在这里可见:http://www.sqlpower.ca/forum/posts/list/2258.page

我已将此补丁应用于最新版本的 MetaData.java github,并将其重新编译为 .class 文件并使用 7-zip 将其复制到 .jar 中,现在它可以很好地与 Hibernate 配合使用。

这是更新后的 java 文件:http://snipt.org/NnH

这是 MetaData.java 文件的前半部分(分成两半,因为它太大了):

/*
 * Copyright (c) 2007 David Crawshaw <david@zentus.com>
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package org.sqlite;

import java.sql.*;
import java.util.Hashtable;

class MetaData implements DatabaseMetaData
{
  private static String sqlQuote(String str) {
    if (str == null) {
      return "NULL";
    }
    int i, single = 0, dbl = 0;
    for (i = 0; i < str.length(); i++) {
      if (str.charAt(i) == '\'') {
        single++;
      } else if (str.charAt(i) == '"') {
        dbl++;
      }
    }
    if (single == 0) {
      return "'" + str + "'";
    }
    if (dbl == 0) {
      return "\"" + str + "\"";
    }
    StringBuffer sb = new StringBuffer("'");
    for (i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if (c == '\'') {
        sb.append("''");
      } else {
        sb.append(c);
      }
    }
    return sb.toString();
  }

    private Conn conn;
    private PreparedStatement
        getTables = null,
        getTableTypes = null,
        getTypeInfo = null,
        getCatalogs = null,
        getSchemas = null,
        getUDTs = null,
        getColumnsTblName = null,
        getSuperTypes = null,
        getSuperTables = null,
        getTablePrivileges = null,
        getExportedKeys = null,
        getProcedures = null,
        getProcedureColumns = null,
        getAttributes = null,
        getBestRowIdentifier = null,
        getVersionColumns = null,
        getColumnPrivileges = null,
        getIndexInfo = null;

    /** Used by PrepStmt to save generating a new statement every call. */
    private PreparedStatement getGeneratedKeys = null;

    MetaData(Conn conn) { this.conn = conn; }

    void checkOpen() throws SQLException {
        if (conn == null) throw new SQLException("connection closed"); }

    synchronized void close() throws SQLException {
        if (conn == null) return;

        try {
            if (getTables != null) getTables.close();
            if (getTableTypes != null) getTableTypes.close();
            if (getTypeInfo != null) getTypeInfo.close();
            if (getCatalogs != null) getCatalogs.close();
            if (getSchemas != null) getSchemas.close();
            if (getUDTs != null) getUDTs.close();
            if (getColumnsTblName != null) getColumnsTblName.close();
            if (getSuperTypes != null) getSuperTypes.close();
            if (getSuperTables != null) getSuperTables.close();
            if (getTablePrivileges != null) getTablePrivileges.close();
            if (getExportedKeys != null) getExportedKeys.close();
            if (getProcedures != null) getProcedures.close();
            if (getProcedureColumns != null) getProcedureColumns.close();
            if (getAttributes != null) getAttributes.close();
            if (getBestRowIdentifier != null) getBestRowIdentifier.close();
            if (getVersionColumns != null) getVersionColumns.close();
            if (getColumnPrivileges != null) getColumnPrivileges.close();
            if (getGeneratedKeys != null) getGeneratedKeys.close();

            getTables = null;
            getTableTypes = null;
            getTypeInfo = null;
            getCatalogs = null;
            getSchemas = null;
            getUDTs = null;
            getColumnsTblName = null;
            getSuperTypes = null;
            getSuperTables = null;
            getTablePrivileges = null;
            getExportedKeys = null;
            getProcedures = null;
            getProcedureColumns = null;
            getAttributes = null;
            getBestRowIdentifier = null;
            getVersionColumns = null;
            getColumnPrivileges = null;
            getGeneratedKeys = null;
        } finally {
            conn = null;
        }
    }

    public Connection getConnection() { return conn; }
    public int getDatabaseMajorVersion() { return 3; }
    public int getDatabaseMinorVersion() { return 0; }
    public int getDriverMajorVersion() { return 1; }
    public int getDriverMinorVersion() { return 1; }
    public int getJDBCMajorVersion() { return 2; }
    public int getJDBCMinorVersion() { return 1; }
    public int getDefaultTransactionIsolation()
        { return Connection.TRANSACTION_SERIALIZABLE; }
    public int getMaxBinaryLiteralLength() { return 0; }
    public int getMaxCatalogNameLength() { return 0; }
    public int getMaxCharLiteralLength() { return 0; }
    public int getMaxColumnNameLength() { return 0; }
    public int getMaxColumnsInGroupBy() { return 0; }
    public int getMaxColumnsInIndex() { return 0; }
    public int getMaxColumnsInOrderBy() { return 0; }
    public int getMaxColumnsInSelect() { return 0; }
    public int getMaxColumnsInTable() { return 0; }
    public int getMaxConnections() { return 0; }
    public int getMaxCursorNameLength() { return 0; }
    public int getMaxIndexLength() { return 0; }
    public int getMaxProcedureNameLength() { return 0; }
    public int getMaxRowSize() { return 0; }
    public int getMaxSchemaNameLength() { return 0; }
    public int getMaxStatementLength() { return 0; }
    public int getMaxStatements() { return 0; }
    public int getMaxTableNameLength() { return 0; }
    public int getMaxTablesInSelect() { return 0; }
    public int getMaxUserNameLength() { return 0; }
    public int getResultSetHoldability()
        { return ResultSet.CLOSE_CURSORS_AT_COMMIT; }
    public int getSQLStateType() { return sqlStateSQL99; }

    public String getDatabaseProductName() { return "SQLite"; }
    public String getDatabaseProductVersion() throws SQLException {
        return conn.libversion();
    }
    public String getDriverName() { return "SQLiteJDBC"; }
    public String getDriverVersion() { return conn.getDriverVersion(); }
    public String getExtraNameCharacters() { return ""; }
    public String getCatalogSeparator() { return "."; }
    public String getCatalogTerm() { return "catalog"; }
    public String getSchemaTerm() { return "schema"; }
    public String getProcedureTerm() { return "not_implemented"; }
    public String getSearchStringEscape() { return null; }
    public String getIdentifierQuoteString() { return " "; }
    public String getSQLKeywords() { return ""; }
    public String getNumericFunctions() { return ""; }
    public String getStringFunctions() { return ""; }
    public String getSystemFunctions() { return ""; }
    public String getTimeDateFunctions() { return ""; }

    public String getURL() { return conn.url(); }
    public String getUserName() { return null; }

    public boolean allProceduresAreCallable() { return false; }
    public boolean allTablesAreSelectable() { return true; }
    public boolean dataDefinitionCausesTransactionCommit() { return false; }
    public boolean dataDefinitionIgnoredInTransactions() { return false; }
    public boolean doesMaxRowSizeIncludeBlobs() { return false; }
    public boolean deletesAreDetected(int type) { return false; }
    public boolean insertsAreDetected(int type) { return false; }
    public boolean isCatalogAtStart() { return true; }
    public boolean locatorsUpdateCopy() { return false; }
    public boolean nullPlusNonNullIsNull() { return true; }
    public boolean nullsAreSortedAtEnd() { return !nullsAreSortedAtStart(); }
    public boolean nullsAreSortedAtStart() { return true; }
    public boolean nullsAreSortedHigh() { return true; }
    public boolean nullsAreSortedLow() { return !nullsAreSortedHigh(); }
    public boolean othersDeletesAreVisible(int type) { return false; }
    public boolean othersInsertsAreVisible(int type) { return false; }
    public boolean othersUpdatesAreVisible(int type) { return false; }
    public boolean ownDeletesAreVisible(int type) { return false; }
    public boolean ownInsertsAreVisible(int type) { return false; }
    public boolean ownUpdatesAreVisible(int type) { return false; }
    public boolean storesLowerCaseIdentifiers() { return false; }
    public boolean storesLowerCaseQuotedIdentifiers() { return false; }
    public boolean storesMixedCaseIdentifiers() { return true; }
    public boolean storesMixedCaseQuotedIdentifiers() { return false; }
    public boolean storesUpperCaseIdentifiers() { return false; }
    public boolean storesUpperCaseQuotedIdentifiers() { return false; }
    public boolean supportsAlterTableWithAddColumn() { return false; }
    public boolean supportsAlterTableWithDropColumn() { return false; }
    public boolean supportsANSI92EntryLevelSQL() { return false; }
    public boolean supportsANSI92FullSQL() { return false; }
    public boolean supportsANSI92IntermediateSQL() { return false; }
    public boolean supportsBatchUpdates() { return true; }
    public boolean supportsCatalogsInDataManipulation() { return false; }
    public boolean supportsCatalogsInIndexDefinitions() { return false; }
    public boolean supportsCatalogsInPrivilegeDefinitions() { return false; }
    public boolean supportsCatalogsInProcedureCalls() { return false; }
    public boolean supportsCatalogsInTableDefinitions() { return false; }
    public boolean supportsColumnAliasing() { return true; }
    public boolean supportsConvert() { return false; }
    public boolean supportsConvert(int fromType, int toType) { return false; }
    public boolean supportsCorrelatedSubqueries() { return false; }
    public boolean supportsDataDefinitionAndDataManipulationTransactions()
        { return true; }
    public boolean supportsDataManipulationTransactionsOnly() { return false; }
    public boolean supportsDifferentTableCorrelationNames() { return false; }
    public boolean supportsExpressionsInOrderBy() { return true; }
    public boolean supportsMinimumSQLGrammar() { return true; }
    public boolean supportsCoreSQLGrammar() { return true; }
    public boolean supportsExtendedSQLGrammar() { return false; }
    public boolean supportsLimitedOuterJoins() { return true; }
    public boolean supportsFullOuterJoins() { return false; }
    public boolean supportsGetGeneratedKeys() { return false; }
    public boolean supportsGroupBy() { return true; }
    public boolean supportsGroupByBeyondSelect() { return false; }
    public boolean supportsGroupByUnrelated() { return false; }
    public boolean supportsIntegrityEnhancementFacility() { return false; }
    public boolean supportsLikeEscapeClause() { return false; }
    public boolean supportsMixedCaseIdentifiers() { return true; }
    public boolean supportsMixedCaseQuotedIdentifiers() { return false; }
    public boolean supportsMultipleOpenResults() { return false; }
    public boolean supportsMultipleResultSets() { return false; }
    public boolean supportsMultipleTransactions() { return true; }
    public boolean supportsNamedParameters() { return true; }
    public boolean supportsNonNullableColumns() { return true; }
    public boolean supportsOpenCursorsAcrossCommit() { return false; }
    public boolean supportsOpenCursorsAcrossRollback() { return false; }
    public boolean supportsOpenStatementsAcrossCommit() { return false; }
    public boolean supportsOpenStatementsAcrossRollback() { return false; }
    public boolean supportsOrderByUnrelated() { return false; }
    public boolean supportsOuterJoins() { return true; }
    public boolean supportsPositionedDelete() { return false; }
    public boolean supportsPositionedUpdate() { return false; }
    public boolean supportsResultSetConcurrency(int t, int c)
        { return t == ResultSet.TYPE_FORWARD_ONLY
              && c == ResultSet.CONCUR_READ_ONLY; }
    public boolean supportsResultSetHoldability(int h)
        { return h == ResultSet.CLOSE_CURSORS_AT_COMMIT; }
    public boolean supportsResultSetType(int t)
        { return t == ResultSet.TYPE_FORWARD_ONLY; }
    public boolean supportsSavepoints() { return false; }
    public boolean supportsSchemasInDataManipulation() { return false; }
    public boolean supportsSchemasInIndexDefinitions() { return false; }
    public boolean supportsSchemasInPrivilegeDefinitions() { return false; }
    public boolean supportsSchemasInProcedureCalls() { return false; }
    public boolean supportsSchemasInTableDefinitions() { return false; }
    public boolean supportsSelectForUpdate() { return false; }
    public boolean supportsStatementPooling() { return false; }
    public boolean supportsStoredProcedures() { return false; }
    public boolean supportsSubqueriesInComparisons() { return false; }
    public boolean supportsSubqueriesInExists() { return true; } // TODO: check
    public boolean supportsSubqueriesInIns() { return true; } // TODO: check
    public boolean supportsSubqueriesInQuantifieds() { return false; }
    public boolean supportsTableCorrelationNames() { return false; }
    public boolean supportsTransactionIsolationLevel(int level)
        { return level == Connection.TRANSACTION_SERIALIZABLE; }
    public boolean supportsTransactions() { return true; }
    public boolean supportsUnion() { return true; }
    public boolean supportsUnionAll() { return true; }
    public boolean updatesAreDetected(int type) { return false; }
    public boolean usesLocalFilePerTable() { return false; }
    public boolean usesLocalFiles() { return true; }
    public boolean isReadOnly() throws SQLException
        { return conn.isReadOnly(); }

    public ResultSet getAttributes(String c, String s, String t, String a)
            throws SQLException {
        if (getAttributes == null) getAttributes = conn.prepareStatement(
            "select "
            + "null as TYPE_CAT, "
            + "null as TYPE_SCHEM, "
            + "null as TYPE_NAME, "
            + "null as ATTR_NAME, "
            + "null as DATA_TYPE, "
            + "null as ATTR_TYPE_NAME, "
            + "null as ATTR_SIZE, "
            + "null as DECIMAL_DIGITS, "
            + "null as NUM_PREC_RADIX, "
            + "null as NULLABLE, "
            + "null as REMARKS, "
            + "null as ATTR_DEF, "
            + "null as SQL_DATA_TYPE, "
            + "null as SQL_DATETIME_SUB, "
            + "null as CHAR_OCTET_LENGTH, "
            + "null as ORDINAL_POSITION, "
            + "null as IS_NULLABLE, "
            + "null as SCOPE_CATALOG, "
            + "null as SCOPE_SCHEMA, "
            + "null as SCOPE_TABLE, "
            + "null as SOURCE_DATA_TYPE limit 0;");
        return getAttributes.executeQuery();
    }

    public ResultSet getBestRowIdentifier(String c, String s, String t,
            int scope, boolean n) throws SQLException {
        if (getBestRowIdentifier == null)
            getBestRowIdentifier = conn.prepareStatement(
            "select "
            + "null as SCOPE, "
            + "null as COLUMN_NAME, "
            + "null as DATA_TYPE, "
            + "null as TYPE_NAME, "
            + "null as COLUMN_SIZE, "
            + "null as BUFFER_LENGTH, "
            + "null as DECIMAL_DIGITS, "
            + "null as PSEUDO_COLUMN limit 0;");
        return getBestRowIdentifier.executeQuery();
    }

    public ResultSet getColumnPrivileges(String c, String s, String t,
                                         String colPat)
            throws SQLException {
        if (getColumnPrivileges == null)
            getColumnPrivileges = conn.prepareStatement(
            "select "
            + "null as TABLE_CAT, "
            + "null as TABLE_SCHEM, "
            + "null as TABLE_NAME, "
            + "null as COLUMN_NAME, "
            + "null as GRANTOR, "
            + "null as GRANTEE, "
            + "null as PRIVILEGE, "
            + "null as IS_GRANTABLE limit 0;");
        return getColumnPrivileges.executeQuery();
    }

    public ResultSet getColumns(String c, String s, String tbl, String colPat)
            throws SQLException {
        Statement stat = conn.createStatement();
        ResultSet rs;
        String sql;

        checkOpen();

        if (getColumnsTblName == null)
            getColumnsTblName = conn.prepareStatement(
                "select tbl_name from sqlite_master where tbl_name like ?;");

        // determine exact table name
        getColumnsTblName.setString(1, tbl);
        rs = getColumnsTblName.executeQuery();
        if (!rs.next())
            return rs;
        tbl = rs.getString(1);
        rs.close();

        sql = "select "
            + "null as TABLE_CAT, "
            + "null as TABLE_SCHEM, "
            + "'" + escape(tbl) + "' as TABLE_NAME, "
            + "cn as COLUMN_NAME, "
            + "ct as DATA_TYPE, "
            + "tn as TYPE_NAME, "
            + "2000000000 as COLUMN_SIZE, "
            + "2000000000 as BUFFER_LENGTH, "
            + "10   as DECIMAL_DIGITS, "
            + "10   as NUM_PREC_RADIX, "
            + "colnullable as NULLABLE, "
            + "null as REMARKS, "
            + "null as COLUMN_DEF, "
            + "0    as SQL_DATA_TYPE, "
            + "0    as SQL_DATETIME_SUB, "
            + "2000000000 as CHAR_OCTET_LENGTH, "
            + "ordpos as ORDINAL_POSITION, "
            + "(case colnullable when 0 then 'N' when 1 then 'Y' else '' end)"
            + "    as IS_NULLABLE, "
            + "null as SCOPE_CATLOG, "
            + "null as SCOPE_SCHEMA, "
            + "null as SCOPE_TABLE, "
            + "null as SOURCE_DATA_TYPE from (";

        // the command "pragma table_info('tablename')" does not embed
        // like a normal select statement so we must extract the information
        // and then build a resultset from unioned select statements
        rs = stat.executeQuery("pragma table_info ('"+escape(tbl)+"');");

        boolean colFound = false;
        for (int i=0; rs.next(); i++) {
            String colName = rs.getString(2);
            String colType = rs.getString(3);
            String colNotNull = rs.getString(4);

            int colNullable = 2;
            if (colNotNull != null) colNullable = colNotNull.equals("0") ? 1:0;
            if (colFound) sql += " union all ";
            colFound = true;

            colType = colType == null ? "TEXT" : colType.toUpperCase();
            int colJavaType = -1;
            if (colType == "INT" || colType == "INTEGER")
                colJavaType = Types.INTEGER;
            else if (colType == "TEXT")
                colJavaType = Types.VARCHAR;
            else if (colType == "FLOAT")
                colJavaType = Types.FLOAT;
            else
                colJavaType = Types.VARCHAR;

            sql += "select "
                + i + " as ordpos, "
                + colNullable + " as colnullable, '"
                + colJavaType + "' as ct, '"
                + escape(colName) + "' as cn, '"
                + escape(colType) + "' as tn";

            if (colPat != null)
                sql += " where upper(cn) like upper('" + escape(colPat) + "')";
        }
        sql += colFound ? ");" :
            "select null as ordpos, null as colnullable, "
            + "null as cn, null as tn) limit 0;";
        rs.close();

        return stat.executeQuery(sql);
    }

数据

关于java - SQLiteJDBC 给 org.sqlite.MetaData.getImportedKeys not yet implemented error with Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2861900/

相关文章:

java - 截去小数点后某一点的双数

java - 错误 :Kotlin: Module was compiled with an incompatible version of Kotlin in a project of Spring Boot with Java not Kotlin not gradle

java - Hibernate:插入回具有相同ID的已删除行

android - 如何将数据插入 Assets 文件夹中的数据库文件

java - 在 Eclipse 中是否有向方法签名添加异常的快捷方式?

java - 如何使用Netbeans连接JAVA MIDLET到ORACLE/MySQL数据库?

java - Tomcat 找不到 H2 内存数据库

java - hibernate 自动发现并生成 POJO 的数据库映射

Android SQlite 到 json

python - sqlite3、Python中 "="和 "match"之间的区别?