首页 > 解决方案 > Spring Boot 同时更新 2 个类

问题描述

我用spring boot开始一个项目,我使用一个有6个表的数据库。这是一个 CRUD 应用程序。

所以,我有 5 个表的实体/dto/service/controller/repository 包。(SQL 中的 6 个表)

现在,我想将实体 x 的表 x(SQL) 列上的一行更新为特定行的另一个实体 y。

在我看来,应该在create X的服务层做,但是怎么做呢?

我应该使用来自 2 个实体的数据创建 xyDTO 吗?我害怕这样做,表 y 它不会自动更新。但是在创建 xyDTO 时。我不想要这个。

如何将特定 DTO x 的数据同时更新到另一个 DTO y(第 6 个 SQL 表)

我在网上找不到类似的例子。有人可以帮我吗?

我的代码:

@Entity
@Table(name = "repo")
public class Repo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    Long id;

    @Column(name="stock")
    private Long stock;
}

@Entity
@Table(name = "voucher")
public class Voucher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "quantity")
    private BigDecimal quantity;
 
    @Column(name = "type")
    private boolean type;
}

@Service
public class VoucherService{
    @Override
    public Voucher dtoToEntity(VoucherDTO dto) {
        Voucher voucher = new Voucher();
        voucher.setId(dto.getId());     
        voucher.setDescription(dto.getDescription());
 
        List<VoucherProduct> voucherList = new ArrayList<>();
    
        for (VoucherProductDTOMini inv : dto.getVoucherproducts()) {
            VoucherProduct voucherL = voucherProductService.DTOtoEntity(inv);        
            voucherList.add(voucherL);
        }
    
        voucher.setVoucherproducts(voucherList);
        return voucher;
    }
    
    @Override
    public VoucherDTO createVoucher(VoucherDTO voucherDTO) {
        Voucher voucher=new Voucher();
        voucher=voucherRepository.save(voucher);

        VoucherDTO voucherDTOnew=new VoucherDTO(voucher);
        return voucherDTOnew;
    }
}

我应该检查我的凭证类型(真实),并且我应该在我的回购实体中添加库存。

我可以通过哪种方式同时更新两个实体?

当我添加一个真正的凭证时,我应该添加我repo.stock的数据voucher.quantity

标签: javaspringspring-bootrestdto

解决方案


First, I would like to highlight a few things in your code:

  1. I cannot find the reason to use @Override annotation on those methods in your Service.
  2. In createVoucher method you create completely empty entity, this is not a good thing to do.

DTO stands for Data Transfer Object, and usually in Spring applications it is used to transfer data to or from the service. For example: Controllers. When user makes a Http Request to receive all Vouchers for example, you would like to return VoucherDto with only those fields that you want users to see.

You can have different DTO objects for Getting entity values and Updating them. Because sometimes you want to allow users to update only certain properties.


To answer your question on how to update two entities in a single call. As I understood your question, you want to update different properties in two different entities via a single request. This is possible, though there are different approaches to this.

  1. Create two different DTOs, one for each entity. Create two different Http Requests each would take one DTO and call a method in a service to update each Entity. ex.: VoucherController.updateVoucher -> VoucherService.updateVoucher and RepoController.updateRepo -> RepoService.updateRepo. I personally prefer this as a solution because your entities Voucher and Repo don't have any relation.
  2. Create a single DTO object, containing all fields required to be updated, then in the service method find your Voucher, and Repo and update their fields, then save both entities. This would be a messy approach when you have many entities.

There would be another different approach if you would have a relation between your Repo and Voucher entities, for example a OneToMany.


Let me know whether this answers your question. There is nothing wrong to have many DTO objects and Services, etc.

If you would like to easily generate all the DTO objects, have a look at this: https://github.com/OpenAPITools/openapi-generator


推荐阅读