Search code examples
hibernaterestjpaspring-data-jpanhibernate-mapping

I am sending Date in the request Body to a REST API post request, but on checking the record inserted in the table, Timestamp is getting inserted


I am trying to Insert certain fields into a table by passing the request body through a POST method

 {
        "sbcVin": "888888",
        "truckIdNum": "222222",
        "toWhen": "02-08-2021",
        "fromWhen": "20-08-2021"
    }

Data is getting inserted too but on checking the records for the column toWhen and FromWhen, I could see some random timings inserted as well. Which shouldn't be there. in My pojo i have declared above two column as Date(SQL) datatype. Below is the pojo.

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "abc")
@SequenceGenerator(name = "abc", sequenceName = "type9", allocationSize = 1)
private int fkeyId;


@Column(name = "SBC_VIN")
private String sbcVin;

@Column(name = "TRUCK_ID_NUM")
private String truckIdNum;


@Column(name = "TO_WHEN")
private Date toWhen;


@Column(name = "FROM_WHEN")
private Date fromWhen;

--- getters and setters

public Date getToWhen() {
        return toWhen;
    }

    public void setToWhen(Date toWhen) {
        this.toWhen = toWhen;
    }

    public Date getFromWhen() {
        return fromWhen;
    }

    public void setFromWhen(Date fromWhen) {
        this.fromWhen=fromWhen;
        
    }


--Controller
@PostMapping("/create")
    public Type create(@RequestBody Type body) {
        String vehicle = body.getVehicle();
        String unit = body.getUnit();

        vehicleList = getVehicleList();
        unitList = getGpsUnit();

        if (vehicleList.contains(vehicle) && unitList.contains(unit)) {
            return repo.save(body);
--------

Data getting saved in DB

02-AUG-2021 05:30:00        20-AUG-2014 05:30:00 --((( i am not supplying this 5:30 anywhere))

Expected data to be saved

02-AUG-2021     20-AUG-2014 ( with out any time stamp)

Question-> 1)why and from where the timestamp is getting generated, as I am not supplying it in the body nor setting it in POJO. 2) I used to think on using Date from java.sql we get only Date , and for timestamp we must use java.sql.TimeStamp or Time.( is my understanding correct and if yes why is Class Date giving us the timestamp.)

Thanks in advance for any Help. :)


Solution

  • I am guessing that, the date stored in your database is 02-AUG-2021 00:00:00 in UTC and 20-AUG-2014 00:00:00 in UTC. Since your timezone is +0530, the time is shown as 02-AUG-2021 05:30:00 and 20-AUG-2014 05:30:00 in Indian Time Zone.

    See, If your database has any data type to store only Date like mysql's Date or you can store plain string.