2016-11-17 72 views
1

得到最后插入的行ID在ebean与SQLUPDATE

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插入行?

+0

使用ORDER BY说明在选择查询 – XING

+0

@XING返回最后插入的记录ID,将只如果主键是自动递增的整数,则工作。 –

回答

0

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

但是这是SQL所以我认为你应该创建USP,你可以为outparm从USP

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';