【问题标题】:JPA : Update operation without JPA query or entitymanagerJPA:没有 JPA 查询或实体管理器的更新操作
【发布时间】:2020-05-25 12:15:29
【问题描述】:

我正在学习 JPA,我发现我们有一些功能已经存在于 Jparepository 中,例如 save、saveAll、find、findAll 等,但没有像 update 这样的功能,

我遇到了一种需要更新表的情况,如果值已经存在,否则我需要在表中插入记录。

我创造了

@Repository
public interface ProductInfoRepository
    extends JpaRepository<ProductInfoTable, String>
{
    Optional<ProductInfoTable> findByProductName(String productname);
}

public class ProductServiceImpl
    implements ProductService
{
    @Autowired
    private ProductInfoRepository productRepository;

    @Override
    public ResponseMessage saveProductDetail(ProductInfo productInfo)
    {
        Optional<ProductInfoTable> productInfoinTable =
            productRepository.findByProductName(productInfo.getProductName());
        ProductInfoTable productInfoDetail;
        Integer quantity = productInfo.getQuantity();
        if (productInfoinTable.isPresent())
        {
            quantity += productInfoinTable.get().getQuantity();
        }
        productInfoDetail =
            new ProductInfoTable(productInfo.getProductName(), quantity + productInfo.getQuantity(),
                                 productInfo.getImage());
        productRepository.save(productInfoDetail);
        return new ResponseMessage("product saved successfully");
    }
}

如您所见,如果记录是新的,我可以保存该记录,但是当我尝试保存表中已经存在的记录时,它会给我与 primarykeyviolation 相关的错误,这很明显。我检查了一下,我们可以通过创建 entitymanager 对象或 jpa 查询来进行更新,但如果我不想同时使用它们怎么办。我们还有其他方法吗?

更新我还添加了EntityManager的实例并尝试合并代码

 @Override
    public ResponseMessage saveProductDetail(ProductInfo productInfo)
    {
        Optional<ProductInfoTable> productInfoinTable =
            productRepository.findByProductName(productInfo.getProductName());
        ProductInfoTable productInfoDetail;
        Integer price = productInfo.getPrice();
        if (productInfoinTable.isPresent())
        {
            price = productInfoinTable.get().getPrice();
        }
        productInfoDetail =
            new ProductInfoTable(productInfo.getProductName(), price, productInfo.getImage());
        em.merge(productInfoDetail);
        return new ResponseMessage("product saved successfully");

但没有错误,日志中没有执行更新语句,有什么可能的原因吗? }

【问题讨论】:

    标签: jpa spring-data-jpa


    【解决方案1】:

    我怀疑你需要这样的代码来解决问题

    public ResponseMessage saveProductDetail(ProductInfo productInfo)
        {
            Optional<ProductInfoTable> productInfoinTable =
                productRepository.findByProductName(productInfo.getProductName());
    
            final ProductInfoTable productInfoDetail;
            if (productInfoinTable.isPresent()) {
                // to edit
                productInfoDetail = productInfoinTable.get();
                Integer quantity = productInfoDetail.getQuantity() + productInfo.getQuantity();
                productInfoDetail.setQuantity(quantity);
            } else {
                // to create new
                productInfoDetail = new ProductInfoTable(productInfo.getProductName(), 
                    productInfo.getQuantity(), productInfo.getImage());
            }
    
            productRepository.save(productInfoDetail);
            return new ResponseMessage("product saved successfully");
        }
    

    【讨论】:

    • 奇怪的行为。正如您在这里看到的github.com/spring-projects/spring-data-jpa/blob/master/src/main/…save 方法对新实例执行persist,对现有实例执行merge。你能提供你的实体类吗?
    • 嘿,不知道如何,但现在工作。我不知道在 jpa 中保存存储库的这种行为。您能否稍微编辑一下您的答案,以便我可以投票并将其标记为答案
    猜你喜欢
    • 2014-08-13
    • 2021-03-27
    • 2016-02-10
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2015-06-22
    相关资源
    最近更新 更多