我正在尝试在Oracle
数据库中插入记录并获取序列ID(也是该表的主键)以供进一步使用。我搜索了SO链接,发现了一些代码片段,并一直在尝试它们,但我在Oracle DB中插入记录时看到了以下错误。
错误
Exception in thread "main" org.springframework.dao.DataRetrievalFailureException: The generated key is not of a supported numeric type. Unable to cast [oracle.sql.ROWID] to [java.lang.Number]
at org.springframework.jdbc.support.GeneratedKeyHolder.getKey(GeneratedKeyHolder.java:73)
at com.ebayenterprise.publicapi.events.dao.DBConnectionUtils.insertEventLogData2(DBConnectionUtils.java:88)
at com.ebayenterprise.publicapi.events.dao.DBConnectionUtils.main(DBConnectionUtils.java:42)
计划方法
private static void insertEventLogData2() {
//setup data
EventType eventType = new EventType(EVENT_TYPE_ID, EVENT_TYPE_CD, null, null);
Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
final EventLog eventLog = new EventLog(eventType, ROUTE_ID, ORDER_ID, null, null, currentTimestamp, currentTimestamp);
System.out.println("eventLog = " + eventLog);
//execute query
GeneratedKeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement statement = con.prepareStatement(INSERT_EVENT_LOG_MESSAGE_SQL, Statement.RETURN_GENERATED_KEYS);
statement.setInt(1, eventLog.getRouteId());
statement.setInt(2, eventLog.getEventType().getEventTypeId());
statement.setString(3, eventLog.getOrderId());
statement.setTimestamp(4, eventLog.getIncomingEventTimestamp());
statement.setTimestamp(5, eventLog.getOutgoingEventTimestamp());
return statement;
}
}, holder);
//get the sequence key
long generatedEventId = holder.getKey().longValue();
System.out.println("generatedEventId = " + generatedEventId);
}
表格
EVENT_ID (Primary Key - Mapped to sequence EVENT_ID_SEQ)
ROUTE_ID
EVENT_TYPE_ID
ORDER_ID
INCOMING_EVENT_TIMESTAMP
OUTGOING_EVENT_TIMESTAMP
答案 0 :(得分:2)
没关系。通过NamedParameterJdbcTemplate得到了它。更清洁的实施。
private static final String INSERT_EVENT_LOG_MESSAGE_SQL2 = "INSERT INTO "
+ "EVENT_LOG (EVENT_ID, ROUTE_ID, EVENT_TYPE_ID, ORDER_ID, INCOMING_EVENT_TIMESTAMP, OUTGOING_EVENT_TIMESTAMP) "
+ "VALUES (EVENT_LOG_ID_SEQ.NEXTVAL, :routeId, :eventTypeId, :orderId, :incomingEventTS, :outgoingEventTS)";
private static void insertEventLogData() {
//setup data
EventType eventType = new EventType(EVENT_TYPE_ID, EVENT_TYPE_CD, null, null);
Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
EventLog eventLog = new EventLog(eventType, ROUTE_ID, ORDER_ID, null, null, currentTimestamp, currentTimestamp);
System.out.println("eventLog = " + eventLog);
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("routeId", ROUTE_ID);
parameters.addValue("eventTypeId", EVENT_TYPE_ID);
parameters.addValue("orderId", ORDER_ID);
parameters.addValue("incomingEventTS", currentTimestamp);
parameters.addValue("outgoingEventTS", currentTimestamp);
GeneratedKeyHolder holder = new GeneratedKeyHolder();
int nb = namedJdbcTemplate.update(INSERT_EVENT_LOG_MESSAGE_SQL2, parameters, holder, new String[]{"EVENT_ID"});
//get the sequence key
long generatedEventId = holder.getKey().longValue();
System.out.println("generatedEventId = " + generatedEventId);
}