JPA:不使用JPA查询或entitymanager的更新操作
我正在学习JPA,我发现我们有一些功能已经存在于JPA存储中,如save、saveAll、find、findAll等。但是没有什么比更新更重要的了 我遇到了一种需要更新表的场景,如果值已经存在,否则我需要在表中插入记录 我创造JPA:不使用JPA查询或entitymanager的更新操作,jpa,spring-data-jpa,Jpa,Spring Data Jpa,我正在学习JPA,我发现我们有一些功能已经存在于JPA存储中,如save、saveAll、find、findAll等。但是没有什么比更新更重要的了 我遇到了一种需要更新表的场景,如果值已经存在,否则我需要在表中插入记录 我创造 @Repository public interface ProductInfoRepository extends JpaRepository<ProductInfoTable, String> { Optional<ProductIn
@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");
}
}
@存储库
公共接口ProductInfoRepository
扩展JPA假设
{
可选findByProductName(String productname);
}
公共类ProductServiceImpl
实现ProductService
{
@自动连线
私有产品信息存储库;
@凌驾
公共响应消息saveProductDetail(ProductInfo ProductInfo)
{
可选产品信息表=
productRepository.findByProductName(productInfo.getProductName());
ProductInfoTableProductInfoDetail;
整数数量=productInfo.getQuantity();
if(productInfoinTable.isPresent())
{
数量+=productInfoinTable.get().getQuantity();
}
productInfoDetail=
新的ProductInfoTable(productInfo.getProductName(),quantity+productInfo.getQuantity(),
productInfo.getImage());
productRepository.save(productInfoDetail);
返回新的响应消息(“产品已成功保存”);
}
}
正如您所看到的,如果记录是新的,我可以保存该记录,但当我试图保存表中已经存在的记录时,它给了我与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");
@覆盖
公共响应消息saveProductDetail(ProductInfo ProductInfo)
{
可选产品信息表=
productRepository.findByProductName(productInfo.getProductName());
ProductInfoTableProductInfoDetail;
整数价格=productInfo.getPrice();
if(productInfoinTable.isPresent())
{
price=productInfoinTable.get().getPrice();
}
productInfoDetail=
新的ProductInfoTable(productInfo.getProductName(),price,productInfo.getImage());
em.merge(productInfoDetail);
返回新的响应消息(“产品已成功保存”);
但是没有错误,日志中没有执行update语句,有什么可能的原因吗?
}我想你需要这样的代码来解决这个问题
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");
}
公共响应消息saveProductDetail(ProductInfo ProductInfo)
{
可选产品信息表=
productRepository.findByProductName(productInfo.getProductName());
最终产品信息表productInfoDetail;
if(productInfoinTable.isPresent()){
//编辑
productInfoDetail=productInfoinTable.get();
整数数量=productInfoDetail.getQuantity()+productInfo.getQuantity();
productInfoDetail.setQuantity(数量);
}否则{
//创造新的
productInfoDetail=新的ProductInfoTable(productInfo.getProductName(),
productInfo.getQuantity(),productInfo.getImage();
}
productRepository.save(productInfoDetail);
返回新的响应消息(“产品已成功保存”);
}
我想你需要这样的代码来解决这个问题
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");
}
公共响应消息saveProductDetail(ProductInfo ProductInfo)
{
可选产品信息表=
productRepository.findByProductName(productInfo.getProductName());
最终产品信息表productInfoDetail;
if(productInfoinTable.isPresent()){
//编辑
productInfoDetail=productInfoinTable.get();
整数数量=productInfoDetail.getQuantity()+productInfo.getQuantity();
productInfoDetail.setQuantity(数量);
}否则{
//创造新的
productInfoDetail=新的ProductInfoTable(productInfo.getProductName(),
productInfo.getQuantity(),productInfo.getImage();
}
productRepository.save(productInfoDetail);
返回新的响应消息(“产品已成功保存”);
}
奇怪的行为。如您所见,这里的save
方法对新实例执行persist
,对现有实例执行merge
。你能提供你的实体类吗?嘿,不知道怎么做,但现在正在工作。我不知道jpa中save for repository的这种行为。请你编辑一下你的答案,这样我就可以投票并将其标记为回答奇怪的行为。如您所见,这里的save
方法对新实例执行persist
,对现有实例执行merge
。你能提供你的实体类吗?嘿,不知道怎么做,但现在正在工作。我不知道jpa中save for repository的这种行为。请你编辑一下你的答案,这样我就可以投票并把这个标记为答案了