如何在 Spring Data 中精美地更新 JPA 实体? [英] How to beautifully update a JPA entity in Spring Data?

查看:34
本文介绍了如何在 Spring Data 中精美地更新 JPA 实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我查看了有关使用 Spring Data 的 JPA 的各种教程,这在很多情况下都不同,我不太确定正确的方法是什么.

So I have looked at various tutorials about JPA with Spring Data and this has been done different on many occasions and I am no quite sure what the correct approach is.

假设有以下实体:

package stackoverflowTest.dao;

import javax.persistence.*;

@Entity
@Table(name = "customers")
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;

@Column(name = "name")
private String name;

public Customer(String name) {
    this.name = name;
}

public Customer() {
}

public long getId() {
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

我们还有一个 DTO,它在服务层被检索,然后交给控制器/客户端.

We also have a DTO which is retrieved in the service layer and then handed to the controller/client side.

package stackoverflowTest.dto;

public class CustomerDto {

private long id;
private String name;

public CustomerDto(long id, String name) {
    this.id = id;
    this.name = name;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

所以现在假设客户想要在 webui 中更改他的名字 - 然后会有一些控制器操作,其中会有带有旧 ID 和新名称的更新的 DTO.

So now assume the Customer wants to change his name in the webui - then there will be some controller action, where there will be the updated DTO with the old ID and the new name.

现在我必须将这个更新的 DTO 保存到数据库中.

Now I have to save this updated DTO to the database.

不幸的是,目前无法更新现有客户(除了删除数据库中的条目并使用新的自动生成的 ID 创建新客户)

Unluckily currently there is no way to update an existing customer (except than deleting the entry in the DB and creating a new Cusomter with a new auto-generated id)

然而,由于这是不可行的(特别是考虑到这样的实体可能有数百个潜在的关系) - 所以我想到了两个直接的解决方案:

However as this is not feasible (especially considering such an entity could have hundreds of relations potentially) - so there come 2 straight forward solutions to my mind:

  1. 为 Customer 类中的 id 创建一个 setter - 从而允许设置 id,然后通过相应的存储库保存 Customer 对象.

  1. 将 id 字段添加到构造函数中,每当您想更新客户时,您总是使用旧 id 创建一个新对象,但其他字段的新值(在本例中仅为名称)

所以我的问题是是否有一般规则如何做到这一点?我解释的两种方法的缺点是什么?

So my question is wether there is a general rule how to do this? Any maybe what the drawbacks of the 2 methods I explained are?

推荐答案

比@Tanjim Rahman 更好的回答你可以使用 Spring Data JPA 使用方法 T getOne(ID id)

Even better then @Tanjim Rahman answer you can using Spring Data JPA use the method T getOne(ID id)

Customer customerToUpdate = customerRepository.getOne(id);
customerToUpdate.setName(customerDto.getName);
customerRepository.save(customerToUpdate);

更好,因为 getOne(ID id) 只获取一个引用(代理)对象,而不是从数据库中获取它.在此参考中,您可以设置您想要的内容,并且在 save() 上,它只会像您期望的那样执行 SQL UPDATE 语句.相比之下,当您像@Tanjim Rahmans 那样调用 find() 时,spring data JPA 将执行 SQL SELECT 以从数据库物理获取实体,当您只是更新时,您不需要它.

Is's better because getOne(ID id) gets you only a reference (proxy) object and does not fetch it from the DB. On this reference you can set what you want and on save() it will do just an SQL UPDATE statement like you expect it. In comparsion when you call find() like in @Tanjim Rahmans answer spring data JPA will do an SQL SELECT to physically fetch the entity from the DB, which you dont need, when you are just updating.

这篇关于如何在 Spring Data 中精美地更新 JPA 实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆