ora-22979: 无法插入对象视图 REF 或用户定义的 REF

ora-22979: cannot INSERT object view REF or user-defined REF

我尝试通过 Java JDBC insert/update 一个 Ref 进入 oracle12c 数据库。

类型定义为:

CREATE OR REPLACE TYPE AngestellterTyp UNDER PersonTyp (
 PersonalNr      INTEGER,
 Rang            VARCHAR(200),
 Einstellung     DATE,
 TelefonBuero    TelefonnummerVarrayTyp,
 Bemerkungen     VARCHAR(2000),
 Vorgesetzter    REF AngestellterTyp
) NOT FINAL;

使用这个更新语句

 public void setVorgesetzter(Long angestellterId, Long vergesetzterId) throws SQLException {
        Ref vorgesetzter = this.getAngestellterRef(vergesetzterId);

        Connection connection = this.dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement("UPDATE ANGESTELLTE an SET an.Vorgesetzter = ? WHERE an.PersonalNr = ?");

        statement.setRef(1, vorgesetzter);
        statement.setInt(2, angestellterId.intValue());

        statement.executeUpdate();
    }

    public Ref getAngestellterRef(Long personalNummer) {
        try {
            return getOracleRef("SELECT REF(a) FROM Angestellte a WHERE a.PersonalNr=" + personalNummer);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Ref getOracleRef(String query) throws SQLException {
       Ref reference = null;
        Connection connection = this.dataSource.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(query);

        if(resultSet.next()) {
            reference = resultSet.getRef(1);
        }
        resultSet.close();
        statement.close();
        return reference;
    }

原因:

ava.sql.SQLException: ORA-22979: Objekt-View-REF oder benutzerdefinierte REF kann nicht mit INSERT eingefügt werden


at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1046)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3714)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1378)

我使用 SQLData 中的 readSQL 和 writeSQL。在 writeSQL 中插入 Ref 会导致相同的异常。

Oracle 建议:

Blockquote Make sure the REF to be inserted is not from an object view or from a user-defined REF column Blockquote

但是如何呢?

解决方案是更改 table 定义:

CREATE TABLE Angestellte OF AngestellterTyp (
CONSTRAINT angestellter_pk PRIMARY KEY (PersonalNr)
) OBJECT IDENTIFIER IS PRIMARY KEY;

至:

CREATE TABLE Angestellte OF AngestellterTyp (
CONSTRAINT angestellter_pk PRIMARY KEY (PersonalNr)
);