Reputation: 3009
I defined an Oracle table this way:
CREATE TABLE MANUAL_CORRECTION
(
ID NUMBER(19,0) NOT NULL,
MODIFIED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
MODIFIED_BY NUMBER(19,0) NOT NULL,
MODIFIED_PROPERTY VARCHAR2(20 BYTE) NOT NULL,
OLD_VALUE VARCHAR2(20 BYTE) NOT NULL,
NEW_VALUE VARCHAR2(20 BYTE) NOT NULL,
CONSTRAINT MODIFIED_BY_FK FOREIGN KEY (MODIFIED_BY) REFERENCES BENUTZER (ID) ENABLE,
PRIMARY KEY (ID)
);
I want to insert records via JPA which works well but the MODIFIED column stays always NULL although I specified that it should be set to the system timestamp by default. How can I achieve that the system timestamp is set whenever a new entity/record is persisted?
Here is how I defined the column/entity property:
@Column(name = "MODIFIED", nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Timestamp modified;
Upvotes: 2
Views: 3114
Reputation: 35900
I think your JPA is sending explicitly NULL as the value for the column and The default value is applied only when you do not specify it in an insert column clause.
Insert into <table> (col1, col2) values (1, null) -- col2 - default value will not be applied
Insert into <table> (col1) values (1) -- col2 - default value will be applied
So if you want to apply default value even when NULL is explicitly passed as a value of the column, you can use the new feature of the Oracle 12c i.e. default on null
Refer this doc
Cheers!!
Upvotes: 2