user1354934 user1354934 - 10 months ago 84
Java Question

Spring JPA + postgres - how do you store a createdOn field?

I am trying to create my model for

as follows, but I am having trouble with
. I've created a column in my postgres db table of data type

How can I have the date reflect the create user date?

Here is snippet of my User class:

public class User {

@GeneratedValue(strategy = GenerationType.AUTO)
private long userid;

private String username;

private String email;

private String firstname;

private String lastname;

private Date createdOn;

public User() {

// getters and setters

Answer Source

You need to specify the kind of Date object you want to store in order to do that you can use @Temporal(TemporalType.TIMESTAMP)

Here you have a few examples of how to use this tag.

In order to reflect the creation date, you can add a default value with a column definition and a default sysdate.

postgresql default values