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

查看:84
本文介绍了如何在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 - 然后会有一些控制器动作,其中将有更新的DTO与旧ID和新名称。

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.

目前还不能更新现有客户(除了删除数据库中的条目并使用新的自动生成创建新的Cusomter) 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对象。 / li>
  1. make a setter for the id in the Customer class - and thus allow setting of the id and then save the Customer object via the corresponding repository.


  1. 将id字段添加到构造函数以及每当要更新cus时你总是用旧的id创建一个新对象,但是其他字段的新值(在这种情况下只是名称)

所以我的问题是,有一般规则如何做到这一点?
任何可能我解释的2种方法的缺点是什么?

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语句。比较当你调用 find()之类的@Tanjim时,Rahmans回答spring数据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天全站免登陆