I have a JPA/EclipseLink model with multiple parameters whose values are set as defaults by the PostgreSQL database. Specifically, I have:
- An
id
column of typeSERIAL
which auto-increments when new rows are added - A
created_at
column of typeTIMESTAMP
which defaults tonow()
My model looks like this:
import javax.persistence.*;
@Entity
@Table(name="entity")
@NamedQuery(name="Entity.findAll", query="SELECT e from Entity e")
public class Entity {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="created_at")
private java.sql.Timestamp createdAt;
// constructor, getters, and setters
}
When I try to insert a row with persist()
in javax.persistence.EntityManager
, the insertion fails because a NULL
value is being set for the created_at
column. Rather than inserting NULL
, I want to simply not insert anything into that column and allow PostgreSQL to set it to now()
. Essentially, I would like to use @GeneratedValue
on createdAt
, but that annotation is only for primary keys and cannot be used on multiple attributes. Is there another annotation I can use that will accomplish this?