使用sqlupdate在ebean中获取最后一个插入的行ID

时间:2016-11-17 14:00:02

标签: sql oracle ebean

如果我使用ebean sqlupdate在Java中插入Oracle

    Transaction tx = Ebean.beginTransaction();
    try {

Transaction tx = Ebean.beginTransaction();
try {

    String sqlString = "INSERT INTO customers VALUES (1001,'Nichols', 'Alexandra', '17 Maple Drive', "+ "'Nashua', 'NH','03062', SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE (-71.48923,42.72347,NULL), NULL, NULL))";
    SqlUpdate query = Ebean.createSqlUpdate(sqlString);
    query.execute();
    String sqlQuery =
            "SELECT @@IDENTITY AS 'Identity'";
    SqlQuery query2 = Ebean.createSqlQuery(sqlQuery);

    List<SqlRow> list = query2.findList();
    System.out.println(list.get(0));
} finally {
  tx.commit();
}

它给出了错误

[PersistenceException: Query threw SQLException:ORA-00936: missing expression
  Query was:
SELECT @@IDENTITY AS 'Identity'

]

如何获取最后插入的行的ID?

1 个答案:

答案 0 :(得分:0)

完成INSERT,SELECT INTO或批量复制语句后,@@ IDENTITY包含该语句生成的最后一个标识值。

但这是在sql所以我认为你应该创建USP并且你可以从USP返回最后插入的记录id作为outparm

   String sqlString = "INSERT INTO customers VALUES (1001,'Nichols', 'Alexandra', '17 Maple Drive', "+ "'Nashua', 'NH','03062', SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE (-71.48923,42.72347,NULL), NULL, NULL))";
   SqlUpdate query = Ebean.createSqlUpdate(sqlString);
   query.execute();
   SELECT @@IDENTITY AS 'Identity';