Search code examples
postgresqlspring-bootjpaspring-restcontrollerspring-boot-2

In Spring Boot with JPA, how do I configure my application so that a date column auto-populates with the current time of it's creation?


I'm using Spring Boot 2 with Java 11. I have created the following JPA entity ...

@Entity
@Table(name = "Mailings")
public class Mailing {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;
    
    @ManyToOne
    @NotNull
    private Card card;
    
    private String message;
    
    @Column(columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private java.sql.Timestamp mailingDate;
    
    @OneToOne(cascade = CascadeType.ALL)
    @NotNull
    private Address senderAddress;
    
    @OneToOne(cascade = CascadeType.ALL)
    @NotNull
    private Address recipientAddress;
}

This is correctly created in my PostGres 10 database when I start my development application ...

cardmania=# \d mailings;
                      Table "public.mailings"
        Column        |            Type             |   Modifiers   
----------------------+-----------------------------+---------------
 id                   | uuid                        | not null
 creation_date        | timestamp without time zone | default now()
 message              | character varying(255)      | 
 card_id              | uuid                        | not null
 recipient_address_id | uuid                        | not null
 sender_address_id    | uuid                        | not null
Indexes:
    "mailings_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "fk5hy4mbv0ewd82t5b8b7shaxkc" FOREIGN KEY (sender_address_id) REFERENCES addresses(id)
    "fk67j1xe6kw5510en1daylstnnn" FOREIGN KEY (card_id) REFERENCES cards(id)
    "fknwiu32uusnxegnulcj1di158k" FOREIGN KEY (recipient_address_id) REFERENCES addresses(id)

Then I created this controller to handle POST requests ...

@RestController
@RequestMapping("/api/mailing")
public class MailingController {

    @Autowired
    private MailingService mailingService;
    
    @PostMapping
    @ResponseStatus(code = HttpStatus.CREATED)
    public void create(@Valid @RequestBody Mailing mailing) {
        mailingService.save(mailing);
    }
    
}

However one issue I am facing is that when I submit POST requests with JSON like the below

{
  "senderAddress": {
    "name": "Joe Recipient",
    "city": "Los Angeles",
    "state": "California",
    "zip_code": "60615",
    "street": "555 Hello Way"
  },
  "recipientAddress": {
    "name": "Joe Recipient",
    "city": "Los Angeles",
    "state": "California",
    "zip_code": "60615",
    "street": "555 Hello Way"
  },
  "card": {
    "id": "05b7af7c-1de5-4a72-aebf-4c9e4d9acec3"
  },
  "message": "Hi"
}

The entity is created correctly in my database but the "creation_date" field is null instead of populating with the current timestamp. Note that the column was generated with a "default now()" modifier, so what else do I need to do in order for my timestamp column to populate properly?


Solution

  • If just want to have a column populated with it's creation timestamp, you can also use @CreationTimestamp:

    @CreationTimestamp
    private Date mailingDate;