简体   繁体   English

无法在SpringBoot应用程序中使用JPA在数据库中保存数据

[英]Unable to save data in database using JPA in SpringBoot application

I'm working on microservice based Java application using JDK 1.8, Spring 5 and SpringBoot 2.0. 我正在使用JDK 1.8,Spring 5和SpringBoot 2.0开发基于微服务的Java应用程序。 I'm using JPA in application for CRUD. 我在申请CRUD时使用了JPA。 I have following method to update an existing record with cart id in database (PostGres) :- 我有以下方法来更新数据库(PostGres)中具有购物车ID的现有记录:-

@Override
@CachePut(value = CommerceConnectorConstants.CommerceCache.COMMERCE_CACHE_TENANT_USER_DATA_MAP)
public Mono<CommerceTenantUser> updateCartId(String tenantId, String userId, String cartId) {
    logger.info("cartId->"+cartId);

    final TenantUserKey commerce = getCommerceObj(tenantId, userId);
    Optional <CommerceTenantUser> tenantUser = commerceTenantUserRepository.findById(commerce);

    return tenantUser.map(tenantUserObj -> updateCartIdAndRefreshMap(tenantUserObj, cartId)).orElse(Mono.empty());

}

private Mono<CommerceTenantUser> updateCartIdAndRefreshMap(CommerceTenantUser tenantUserObj, String cartId) {
    tenantUserObj.setCartId(cartId);

    final Mono<CommerceTenantUser> commerceTenantUser = asyncRunner
            .one(() -> commerceTenantUserRepository.saveAndFlush(tenantUserObj))
            .doOnNext(value -> commerceCacheService.refreshMap())
            .doOnError(error -> logger.error("Error while persisting Cart Id: {}", error))
            .map(commerceTenantUserObj -> commerceTenantUserObj);

    return commerceTenantUser;

}

My issue is in one of the application flow, I'm unable to update the cart id. 我的问题在于应用程序流程之一,我无法更新购物车ID。 I tried to debug the flow many times, I can clearly see that the call is happening to this method and I can log cart id value in console as well but in the end the update of cart id never actually happens in database. 我尝试了多次调试流程,可以清楚地看到此方法正在发生调用,并且我也可以在控制台中记录购物车ID值 ,但最终,购物车ID的更新实际上从未在数据库中发生。 Please note this same method for updating the cart id is happening in other flows of application where cart id eventually gets saved in database (except my flow) so I guess there is no issue with the implementation logic of saving. 请注意,在其他应用程序流中也会使用相同的方法来更新购物车ID,其中购物车ID最终会保存在数据库中(我的流程除外),因此我想保存的实现逻辑没有问题。

Following is the flow where this method invocation is happening but not saving in database :- 以下是发生此方法调用但未保存在数据库中的流程:

@Override public Mono verifyCredentialsforBasicAuthorization(Map requestInfo) { @Override public Mono verifyCredentialsforBasicAuthorization(Map requestInfo){

Tuple4<String, String, WebClient, String> serviceConnectionDetails = commerceConnectorHelper.verifyCredentialsforBasicAuthorization(requestInfo);

if(!StringUtils.isBlank(serviceConnectionDetails._1)) {
    logger.info("Calling cart id update service...");

       return serviceConnectionDetails._3
    .post()
         .uri(builder -> builder.path(serviceConnectionDetails._1)
             .queryParam(CommerceConnectorConstants.CartParams.OLD_CART_ID, serviceConnectionDetails._4)
             .build())
    .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
    .retrieve()
    .bodyToMono(CartModel.class)
         .map(response ->
         {
           return repositoryDetails.updateCartId(requestInfo.get(CommerceConnectorConstants.HttpHeaders.TENANT_ID).get(),
               requestInfo.get(CommerceConnectorConstants.HttpHeaders.TENANT_USER_ID).get(), response.getGuid()).then(Mono.just(response));
         }) .then(repositoryDetails.updateCommerceUserId(requestInfo.get(CommerceConnectorConstants.HttpHeaders.TENANT_ID).get(),
             requestInfo.get(CommerceConnectorConstants.HttpHeaders.TENANT_USER_ID).get(),
             requestInfo.get(CommerceConnectorConstants.AuthorizationParams.AUTHORIZATION_USERNAME).get()))

     .then(serviceConnectionDetails._3
         .get()
         .uri(serviceConnectionDetails._2)
         .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
         .retrieve()
         .bodyToMono(UserProfile.class)
         .doOnNext(value -> logger.info("Called User Profile service {}", value.getCustomerId()))
         .doOnError(error -> logger.error("Error while calling User Profile service: {}", error)));


}
else
//Update Commerce User Id
;

//User Profile Call
return serviceConnectionDetails._3
    .get()
    .uri(serviceConnectionDetails._2)
    .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
    .retrieve()
    .bodyToMono(UserProfile.class)
    .doOnNext(value -> logger.info("Called User Profile service {}", value.getCustomerId()))
        .doOnError(error -> logger.error("Error while calling User Profile service: {}", error));

} }

If you notice, I'm calling this method repositoryDetails.updateCartId and passing all the required parameters including the cart id by using response.getGuid() . 如果您注意到,我将调用此方法repositoryDe​​tails.updateCartId并通过使用response.getGuid()传递所有必需的参数,包括购物车ID。 The method is executed in the end but eventually it does not saves the data in table. 该方法最后执行,但最终不会将数据保存在表中。 I tried really hard but not able to figure out the issue. 我非常努力,但无法找出问题所在。 This is the problem with asynchronous calls using the Java Reactive programming. 这是使用Java Reactive编程进行异步调用的问题。 I would really appreciate if you can help me in figuring out issue. 如果您能帮助我解决问题,我将不胜感激。 Thanks 谢谢

I found the root cause of it. 我找到了根本原因。 I was suppose to call update once only on same Entity using JPA. 我想只使用JPA在同一实体上调用一次update。 In my case I was calling twice individually to update 2 different columns in same Entity. 以我为例,我分别调用两次以更新同一实体中的2个不同的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM