I have a JPA/EclipseLink model with multiple parameters whose values are set as defaults by the PostgreSQL database. Specifically, I have:
id
column of type SERIAL
which auto-increments when new rows are addedcreated_at
column of type TIMESTAMP
which defaults to now()
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?
You may want insertable=false
in the Column
annotation:
@Column(name="created_at", insertable=false)
private java.sql.Timestamp createdAt;
From: http://docs.oracle.com/javaee/5/api/javax/persistence/Column.html
boolean insertable (Optional) Whether the column is included in SQL INSERT statements generated by the persistence provide