英文:
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<Order, Long> {
Optional<Order> 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 <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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论