juststarted
juststarted

Reputation: 35

Jpa Update tables get Null

I'm kind a new in programming and I'm trying to update tables with requestDto that have multiple entities with one request. Each tables mapped by Invoice tables, has Foreign Keys But I got Null from other tables when I PUT update.

This is Service, I only put Invoice and Seller but get null from Seller
@Transactional
    public Long update(Long id, InvoiceUpdateRequestDto requestDto){

        Invoice invoice = invoiceRepository.getOne(id);
        invoice.update(requestDto.getNote(), requestDto.getSeller());
        Seller seller = new Seller();
        seller.setSellerManagerNumber(requestDto.getSeller().getSellerManagerNumber());
        sellerRepository.save(seller);
    return id;
    }

I made inner Dto,

@NoArgsConstructor
@Getter
public class InvoiceUpdateRequestDto {

    private String note;
    private boolean finalized;
    private DeliveryUpdateRequest delivery;
    private ProductUpdateRequest product;
    private BuyerUpdateRequest buyer;
    private SellerUpdateRequest seller;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class DeliveryUpdateRequest  {
        private String destination;
        private String address;
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ProductUpdateRequest {
        private String productNumber;
        private String productName;
        private int stockQuantity;
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class BuyerUpdateRequest  {
        private String companyName;
        private String buyerManager;
        private String buyerManagerNumber;
        private String faxNumber;
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SellerUpdateRequest  {
        private String companyName;
        private String sellerManager;
        private String sellerManagerNumber;
        private String faxNumber;
    }

this is invoice entity

@Getter
@Entity
public class Invoice extends BaseEntity {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "invoice_id")
    private Long id;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "buyer_id")
    private Buyer buyer;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "seller_id")
    private Seller seller;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "product_id")
    private Product product;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "delivery_id")
    private Delivery delivery;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "orderItem_id")
    private OrderItem orderItem;

    private boolean finalized;

    public boolean isFinalized() {
        return finalized;
    }
    public Invoice(){
    }
    private String note;


//I put only Seller for now to test it.
public void update(String note, Seller seller){
        this.note= note;
        this.seller = seller;
    }

this is controller

@PutMapping("/api/order/{id}/update") //
    public ResponseEntity<HashMap<String, String>> updateInvoice(@PathVariable Long id, @RequestBody InvoiceUpdateRequestDto requestDto) {
        HashMap<String, String> result = new HashMap<>();
        result.put("message", "Success");
        return ResponseEntity.ok(invoiceService.update(id,requestDto)).status(HttpStatus.OK).body(result); }

I think I made a mistake in Service. Could you tell me what I missed?

Upvotes: 0

Views: 79

Answers (1)

deepakchethan
deepakchethan

Reputation: 5600

You are trying to save SellerUpdateRequest into the invoice entity. Create Seller out of the request and set it in invoice. And I don't think you are saving the invoice after updating the entity, You try this:

@Transactional
public Long update(Long id, InvoiceUpdateRequestDto requestDto){
    Seller seller = new Seller();
    seller.setSellerManagerNumber(requestDto.getSeller().getSellerManagerNumber());
    // save rest of the fields of seller
    sellerRepository.save(seller); 
    Invoice invoice = invoiceRepository.getOne(id);
    invoice.update(requestDto.getNote(), seller);
    invoiceRepository.save(invoice);  // save the invoice here
    return id;
}

Upvotes: 2

Related Questions