Java Spring:使用存储过程更新记录后获取更新记录

时间:2019-05-09 07:39:32

标签: java spring

我正在使用Java Spring进行一个项目,以做一个API REST,该REST连接到SQL Server数据库存储过程以进行插入,更新和删除。我可以使用插入过程创建新记录,也可以使用更新过程来修改它们。

我遇到的问题是,在更新记录后,它将对更新后的记录进行“按ID获取”,但是它会在更新发生之前返回包含数据的记录。

自定义存储库

public class Catcategoryl1RepositoryImpl implements Catcategoryl1RepositoryCustom {
    private static final String PK_CATEGORY_L1 = "PK_CategoryL1";
    private static final String CATEGORY_L1_NAME = "CategoryL1Name";
    private static final String FK_PRODUCT_TYPE = "FK_ProductType";
    private static final String FK_TAX_RANGE = "FK_TaxRange";
    private static final String LOSS_ACCOUNT = "LossAccount";
    private static final String PROFIT_ACCOUNT = "ProfitAccount";
    private static final String SW_CORPORATE = "swCorporate";
    private static final String STATUS = "Status";
    private static final String FK_USER = "FK_User";
    private static final String SW_CHECK_WARNING = "swCheckWarning";
    private static final String OPTIMISTIC_LOCKING = "OptimisticLocking";

    @PersistenceContext
    private EntityManager em;

    @Transactional
    @Override
    public Map<ResultsParameter, Object>  updateProcedure(Catcategoryl1 catcategoryl1) {
        StoredProcedureQuery sp = em.createStoredProcedureQuery("spApiCatCategoryL1Put001")
        .registerStoredProcedureParameter(PK_CATEGORY_L1, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(CATEGORY_L1_NAME, String.class, ParameterMode.IN)
        .registerStoredProcedureParameter(FK_PRODUCT_TYPE, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(FK_TAX_RANGE, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(LOSS_ACCOUNT, String.class, ParameterMode.IN)
        .registerStoredProcedureParameter(PROFIT_ACCOUNT, String.class, ParameterMode.IN)
        .registerStoredProcedureParameter(SW_CORPORATE, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(STATUS, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(FK_USER, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(OPTIMISTIC_LOCKING, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(SW_CHECK_WARNING, Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter(ResultsParameter.CODE.getValue(), Integer.class, ParameterMode.OUT)
        .registerStoredProcedureParameter(ResultsParameter.DESCRIPTION.getValue(), String.class, ParameterMode.OUT)
        .registerStoredProcedureParameter(ResultsParameter.UNIQUE_KEY.getValue(), Integer.class, ParameterMode.OUT)
        .setParameter(PK_CATEGORY_L1, catcategoryl1.getPkCategoryl1())
        .setParameter(CATEGORY_L1_NAME, catcategoryl1.getCategoryl1name())
        .setParameter(FK_PRODUCT_TYPE, catcategoryl1.getFkProducttype())
        .setParameter(FK_TAX_RANGE, catcategoryl1.getFkTaxrange())
        .setParameter(LOSS_ACCOUNT, catcategoryl1.getLossaccount())
        .setParameter(PROFIT_ACCOUNT, catcategoryl1.getProfitaccount())
        .setParameter(SW_CORPORATE, catcategoryl1.getSwCorporate())
        .setParameter(STATUS, catcategoryl1.getStatus())
        .setParameter(FK_USER, 1)
        .setParameter(OPTIMISTIC_LOCKING, catcategoryl1.getOptimisticlocking())
        .setParameter(SW_CHECK_WARNING, catcategoryl1.getSwCheckWarning());

        // execution of SP and get of params
        sp.execute();

        Map<ResultsParameter, Object> mapResult = new EnumMap<>(ResultsParameter.class);
        mapResult.put(ResultsParameter.CODE, sp.getOutputParameterValue(ResultsParameter.CODE.getValue()));
        mapResult.put(ResultsParameter.DESCRIPTION, sp.getOutputParameterValue(ResultsParameter.DESCRIPTION.getValue()));
        mapResult.put(ResultsParameter.UNIQUE_KEY, sp.getOutputParameterValue(ResultsParameter.UNIQUE_KEY.getValue()));

        return mapResult;
    }
}

服务实施

@Service("catcategoryl1Service")
public class Catcategoryl1ServiceImpl implements Catcategoryl1Service {

@Autowired
Catcategoryl1Repository catcategoryl1Repository;

@Override
public Optional<Catcategoryl1> update(Catcategoryl1 catcategoryl1) {
    if (catcategoryl1 == null || catcategoryl1.getPkCategoryl1() == null || catcategoryl1.getPkCategoryl1() == 0) {
        throw new BadRequestException(400, "Not.Found");
    }
    if (!catcategoryl1Repository.findById(catcategoryl1.getPkCategoryl1()).isPresent()) {
        throw new NotFoundException(400, "Could not retrieve the result object");
    }
    // GET THE ID OF THE UPDATED RECORD
    Map<ResultsParameter, Object> result = catcategoryl1Repository.updateProcedure(catcategoryl1);

    if (Objects.nonNull(result.get(ResultsParameter.CODE))
            && Objects.nonNull(result.get(ResultsParameter.DESCRIPTION))
            && Objects.nonNull(result.get(ResultsParameter.UNIQUE_KEY))) {

        if ((int)(result.get(ResultsParameter.CODE)) == 0) {
            // RETURN OF THE UPDATED RECORD (SELECT) FAIL -> returns previous data
            Optional<Catcategoryl1> obj = catcategoryl1Repository.findById((int)(result.get(ResultsParameter.UNIQUE_KEY)));
            return obj;
        }
        throw new InternalServerErrorException(result);
    }
    throw new InternalServerErrorException(500, "Unknown error");
}
}

我已搜索有关此问题的信息,但找不到任何信息。

0 个答案:

没有答案