如何使用Spring Data JPA实现一个保存或更新功能?

huangapple go评论63阅读模式
英文:

How can I implement a Save Or Update function using Spring Data Jpa?

问题

我正在使用Spring Data JPA将记录保存到数据库中。
以下是我使用的方法:

    @Override
    public void persistOrder(final Order order) {
        orderRepository.save(order);
    }

以及我的仓库:

    @Repository
    public interface OrderRepository extends JpaRepository<Order, Long> {
        Optional<Order> findByOrderId(UUID orderId);
    }

我想修改它,使其能够保存或更新。目前我已经做到了这一点:

    @Override
    public void saveOrUdpate(final Order order) {
      orderRepository.save(order);
      long id = orderRepository.findByOrderId(order.get()).getId();
      orderRepository.deleteById(id);
      orderRepository.save(order);
    }

我有一些问题 - <br>
#1 这样做是否不好(删除然后重新插入)? <br>
#2 如果我使用Hibernate实现我的仓库,Hibernate的saveOrUpdate(..)会在幕后执行相同的操作吗? <br>
#3 您有没有想法如何只使用纯Spring Data JPA来实现这一点? <br>
<br>
谢谢 :) <br>
<br>
英文:

I am using Spring Data JPA to save a record to my database.
Here is the method I am using:

@Override
public void persistOrder(final Order order) {
    orderRepository.save(order);
}

and my repository:

@Repository
public interface OrderRepository extends JpaRepository&lt;Order, Long&gt; {
    Optional&lt;Order&gt; findByOrderId(UUID orderId);
}

I want to modify this so it will Save or Update. This is what I have so far:

@Override
public void saveOrUdpate(final Order order) {
  orderRepository.save(order);
  long id = orderRepository.findByOrderId(order.get()).getId();
  orderRepository.deleteById(id);
  orderRepository.save(order);
}

I have a few questions - <br>
#1 Is this bad practice (deleting and then re-inserting)? <br>
#2 If I implement my repo using hibernate, would a hibernate saveOrUpdate(..) do the same thing under the hood? <br>
#3 Do you have an idea how I can achieve this just using plain Spring Data Jpa? <br>
<br>
Thanks 如何使用Spring Data JPA实现一个保存或更新功能? <br>
<br>

答案1

得分: 1

Update::<br>
好的,谢谢评论,我决定添加一个单独的更新方法!我认为现在看起来更好了!

 @Override
 public void updateOrder(Order order) {
    long id = orderRepository.findByOrderId(order.get().getId());
    order.setId(id);
    orderRepository.save(order);
}

关于 Spring Data JPA 的 save(..) 方法默认执行 upsert,提出了一个很好的观点。但是,不幸的是,在我正在处理的项目中,主键是自动生成的。这意味着每当在现有记录上执行 save(..) 以进行更新时,都会创建一个新记录!这就是为什么我在这里明确设置了 id,以避免发生这种情况!<br><br>我还在实体类的 orderId 列(非主键)上添加了一个 eclipse-link 库的 @Index 注释,以加速更新方法的 findByOrderId!

英文:

Update::<br>
Ok, thanks for the comments, I've decided to add a separate update method! I think this is looking better!

 @Override
 public void updateOrder(Order order) {
    long id = orderRepository.findByOrderId(order.get().getId());
    order.setId(id);
    orderRepository.save(order);
}

A good point was made about Spring Data JPA's save(..) method doing an upsert by default. But, unfortunately in the project I'm working on the primary keys are autogenerated. This means that whenever save(..) is executed on an existing record to update it, a new record is created instead! That's why I explicitly set the id here to avoid that happening!<br><br>I've also added and eclipse-link library @Index annotation to the orderId column (not primary key) in the entity class to speed up the update method's findByOrderId!

huangapple
  • 本文由 发表于 2020年9月26日 20:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64077558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定