0
votes

I am getting NPE while getting back the auto incremented key in oracle 12c. I am using ojdbc7.jar downloaded from oracle site for oracle 12c. Version - 12.1.0.1.0. Here is the stack trace.

java.lang.NullPointerException
at oracle.jdbc.driver.AutoKeyInfo.initMetaDataKeyFlag(AutoKeyInfo.java:404)
at oracle.jdbc.driver.AutoKeyInfo.initMetaData(AutoKeyInfo.java:392)
at oracle.jdbc.driver.OracleReturnResultSet.getMetaData(OracleReturnResultSet.java:77)
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getMetaData(DelegatingResultSet.java:322)
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getMetaData(DelegatingResultSet.java:322)
at org.springframework.jdbc.core.ColumnMapRowMapper.mapRow(ColumnMapRowMapper.java:52)

I am using spring jdbc and its keyHolder to get the key back.While in oracle side using sequence to generate the id.

Read somewhere in hibernate forum, its a bug in jdbc driver itself but the oracle forum is restricted for me. Hibernate forum link. Anyone having same problem and how they are tackling this issue.

Sample code :

public Double insert(Definition definition) {
    final String name = definition.getName();
    final String desc = definition.getDesc();
    final String type= definition.getType();
    final String insertSql = "INSERT INTO DEFINITION (ID, TYPE, NAME, DESC) VALUES (MY_SEQ.NEXTVAL,?,?,?)";
    KeyHolder holder = new GeneratedKeyHolder();

    getJdbcTemplate().update(new PreparedStatementCreator() {           

                    @Override
                    public PreparedStatement createPreparedStatement(Connection connection)
                            throws SQLException {
                        PreparedStatement ps = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);
                        ps.setString(1, type);
                        ps.setString(2, name);
                        ps.setString(3, desc);
                        return ps;
                    }
                }, holder);

    Double generatedId = holder.getKey().doubleValue();
    return generatedId;
}
1
Please show the relevant code.Jens
which JDBC driver version are you using? You can get that using DatabaseMetaData.getDriverVersion() (the number in the jar file's name is not the driver version)a_horse_with_no_name
Update the question by adding the sample code and driver info.VGaur
"ojdbc7.jar" is not a version, it means it is for Java 7. Which version of the driver are you using?Mark Rotteveel
I'm not 100% sure here but maybe you should use INSERT INTO ... VALUES ... RETURING. The RETURNING clause is intended for this purpose. Also do not convert Oracle NUMBER into Double. You should use BigDecimal for identifiers.ibre5041

1 Answers

2
votes

It has been fixed in driver version 12.2. When that is public you will have a fix, else you may contact oracle support and ask for a driver patch.

Still waiting for the public release of 12.2