Indrajeet Gour
Indrajeet Gour

Reputation: 4490

Spring Jpa LessThanEqual for postgres timestamp field

I have field effective_startdate in my Entity class.

@Basic
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "effective_startdate", columnDefinition = "TIMESTAMP WITHOUT TIME ZONE")
private Date effectiveStartdate;

Which i wanted to use in my Jpa repository class for filtering into my getmapping method for further use:

List<PoJoObject> findByEffectiveStartdateLessThanEqual(Date date);

For this entire operation my backend db is postgres and the same field there is having type as TIMESTAMP.

Would anyone can help me to get the expected format("yyyy-MM-dd HH:mm:ss") response from the my getmapping call.

I am totally new to this and did not know how the Spring is internally dealing with Date and serializing/deserializing and comparing with postgres db.

Note:

2019-09-19 18:28:23.012

"effectiveStartdate": "2019-07-30T04:37:55.000+0000"

Upvotes: 0

Views: 3560

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36133

You have to add the JsonFormat annotation to the effectiveStartdate:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS")
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "effective_startdate", columnDefinition = "TIMESTAMP WITHOUT TIME ZONE")
private Date effectiveStartdate;

Btw. the @Basic annotation is not necessary and the column name is also superfluous because this is how Hibernate translates _ to camelCase.

Upvotes: 1

Related Questions