user1354934 user1354934 - 2 days ago 5
Java Question

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

I am trying to create my model for

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

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

Here is snippet of my User class:

@Entity
@Table(name="users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="userid")
private long userid;

@NotNull
@Column(name="username")
private String username;

@NotNull
@Column(name="email")
private String email;

@NotNull
@Column(name="firstname")
private String firstname;

@NotNull
@Column(name="lastname")
private String lastname;

@Column(name="createdOn")
private Date createdOn;

public User() {
}

// getters and setters
}

Answer

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

Comments