首页 > 解决方案 > 如何获取我刚刚插入数据库的自动生成的 ID (Java)

问题描述

我知道有类似的问题,但我在 java 中找不到它,这是我的问题,我有一个名为“transactions”的表,它有一个自动生成的 Id [COT_Id],我有一个中间表 [USER_TRANS],其中包含以下列:[ COT_Id], [USER_Id] 在事务表和用户表之间建立关系,我需要在事务中插入一个新值,它已经完成,现在一旦插入,我需要获取自动生成的 ID 以便将其插入中间表中,这是我的代码:

  Transaction transaccion = new Transaction();
                 transaccion.setMvg_id(mvg_id);
                 aproCvv = generalService.getCardByCvv(cvv, cardNum);
                 transaccion.setCrd_id(aproCvv);
                 transaccion.setCot_amount(cot_amount);
                 transaccion.setCot_cuotas(cuotas);
                 transaccion.setCcr_id(ccr_id);
                 transaccion.setCot_authorizationNumber(numeroauto);
                 Date dateAct = new Date();
                 transaccion.setCot_CreateDate(dateAct);
                 transaccion.setEsb_id(esb_id);
                 transaccion.setCot_Ip(ipRemoto);
                 transaccion.setCot_cashUser(cot_cashUser);
                 transaccion.setCot_Reference(cot_Reference);
                 transaccion.setChn_id(chn_id);
                 transaccion.setCot_reverseReference("5454");
                 transaccion.setCot_transactionPin("566546df");
                 transaccion.setTrs_id(54);

                 if(resultado) {

                     transactionService.saveTrans(transaccion);

                    //here's where I need the autogenerated Id (transId)
                     generalService.saveUserTrans(udID, transId, 1);   

标签: javasqlsql-serverhibernatejpa

解决方案


推荐阅读