Skip to content

Commit 8f50678

Browse files
committed
fix:解决数据源为Oracle时,使用自增主键,获取不到插入的主键问题
解决数据源为Oracle时,使用自增主键,获取不到新增后的id问题 报错信息: getLong not implemented for class oracle.jdbc.driver.T4CRowidAccessor issue #338
1 parent f3e7ed1 commit 8f50678

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,13 @@ public PreparedStatement getStatement(@NotNull SQLConfig config, String sql) thr
10681068

10691069
PreparedStatement statement; //创建Statement对象
10701070
if (config.getMethod() == RequestMethod.POST && config.getId() == null) { //自增id
1071-
statement = getConnection(config).prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
1071+
if (config.isOracle()) {
1072+
// 解决 oracle 使用自增主键 插入获取不到id问题
1073+
String[] generatedColumns = {config.getIdKey()};
1074+
statement = getConnection(config).prepareStatement(sql, generatedColumns);
1075+
} else {
1076+
statement = getConnection(config).prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
1077+
}
10721078
}
10731079
else if (RequestMethod.isGetMethod(config.getMethod(), true)) {
10741080
statement = getConnection(config).prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
@@ -1250,7 +1256,7 @@ public int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception
12501256
if (config.getId() == null && config.getMethod() == RequestMethod.POST) { // 自增id
12511257
ResultSet rs = stt.getGeneratedKeys();
12521258
if (rs != null && rs.next()) {
1253-
config.setId(rs.getLong(1)); //返回插入的主键id FIXME Oracle 拿不到
1259+
config.setId(rs.getLong(1));
12541260
}
12551261
}
12561262

0 commit comments

Comments
 (0)