0

Im trying to save product entity in postgres table, and if i send request from postman, it returns sved object, but MRP field is always 0.0

Entity class:

package com.acms.models;

import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "product")
public class Product {
    @Id
    @Column(name = "productid")
    private String productId;
    @Column(name = "timestamp")
    private long timeStamp;
    private String description;
    private String name;
    @Column(name = "mrp")
    private double mRP;
    private int quantity;
    private double promotion;

    public Product() {
        super();
        long epochTime = Instant.now().getEpochSecond();
        this.timeStamp = epochTime;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

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

    public double getMRP() {
        return mRP;
    }

    public void setMRP(double mRP) {
        this.mRP = mRP;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getPromotion() {
        return promotion;
    }

    public void setPromotion(double promotion) {
        this.promotion = promotion;
    }

    @Override
    public String toString() {
        return "Product [productId=" + productId + ", timeStamp=" + timeStamp + ", description=" + description
                + ", name=" + name + ", MRP=" + mRP + ", quantity=" + quantity + ", promotion=" + promotion + "]";
    }
}

Controller:

@PostMapping("/product/save")
    public Product postDataToProductTable(@RequestBody Product product) {
        return this.productService.postDataToProductTable(product);
    }

Service:

public Product postDataToProductTable(Product product) {
        return this.productRepository.save(product);
    }

Request sent with url and object:

http://localhost:8080/product/save
{
    "productId":"p106",
    "description":"butter",
    "name":"amul",
    "mRP":"200",
    "quantity":"8",
    "promotion":"5.0"
}

Object returned:

{
    "productId": "p106",
    "description": "butter",
    "name": "amul",
    "quantity": 8,
    "promotion": 5.0,
    "mrp": 0.0
}

notice how above, the mrp is returned as 0.0, but all other fields are fine.

Table in postgres:

CREATE TABLE product (
    serialNumber SERIAL,
    productID VARCHAR(50) NOT NULL,
    timeStamp bigint NOT NULL,
    description VARCHAR(50),
    name VARCHAR(50) NOT NULL,
    MRP NUMERIC NOT NULL,
    quantity int NOT NULL,
    promotion NUMERIC,
    PRIMARY KEY(productID),
    UNIQUE(productID, timeStamp)
);

1 Answers1

1

Possibly because your JSON request sends mRP as a String type, but in your model it's defined as a double. Lose the "" around mRP's value in the JSON request and see if that helps.

Another option is that you defined this type as numeric in the database, but in the model it's a double. In my experience I would prefer to use a BigDecimal to map to a numeric field in postgres. But if it must be a double in your model then why not use that as a type in the database as well? Not everything that can be saved in a numeric field can actually be represented in a Java double.

Postgres data types: https://www.postgresql.org/docs/9.1/datatype-numeric.html

Also (from comments discussion) inconsistent casing between the Json and the field name in the model and could be causing issues if using Spring Data JPA/Spring MVC.

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73