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;
}
DatabaseMetaData.getDriverVersion()
(the number in the jar file's name is not the driver version) – a_horse_with_no_nameINSERT INTO ... VALUES ... RETURING
. TheRETURNING
clause is intended for this purpose. Also do not convert Oracle NUMBER into Double. You should use BigDecimal for identifiers. – ibre5041