Carey Lynda
Carey Lynda

Reputation: 15

Spring Boot Update at 2 Classes at the same time

I start a project with spring boot, I use a database with 6 tables. It is a CRUD application .

So , I have my entities/dto/service/controller/repository packages for 5 tables. (6 tables in SQL)

Now, I would like to update a row on the column of table x(SQL) of entity x to another entity y at a specific row.

In my opinion, it should be done at the service layer at create X, but how?

Should I create a xyDTO with data from 2 entities? I am afraid of doing it , the table y it doesn't update automatically.But when create the xyDTO. I don't want this.

How I can update the data of specific DTO x to another DTO y (6th table of SQL), at the same time

I cannot find similar example online. Could anyone help me?

My code:

@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;
    }
}

I should check the type of my voucher (true), and I should add on my repo entity at stock.

In which way can I update the two entities at the same time?

When I add a true voucher, I should add on my repo.stock the data of voucher.quantity.

Upvotes: 0

Views: 1032

Answers (1)

Markiian Benovskyi
Markiian Benovskyi

Reputation: 2161

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

Upvotes: 1

Related Questions