Take the 2-minute tour ×

I have a domain object and annotated as follows

 @Table(name = "REQUEST")
 public class Request {

   * Unique id for this request
@Column(name = "EQ_ID")
private long requestId;
@Column(name = "EMAIL_ID")
private String emailId;
@Column(name = "REQUEST_DATE")
private Date requestDate;
*Getters/setters omitted

The column Request_date cannot be null and as per the DDL the default value is sysdate (oracle DB). How do I annotate this field so that if the requestDate property is null,hiberanate automatically inserts sysdate.? Currently it throws error when the field is null,which is very obvious as it cannot be null as per the DB constraints. How do I go about this? One alternative is to mark this field as transient and the inserts work fine. But the negative aspect is that, I will not be able to retrieve the value (of request_date column).

share|improve this question

5 Answers 5

up vote 7 down vote accepted

This is a missing feature in hibernate annotations. Also there exist some workaround as Yok has posted. The problem is that the workaround is vendor dependent and might not work for all DB. In my case,Oracle, it isn't working and has been reported as a bug.

share|improve this answer

you can put default value in columnDefination. Something similar to

   @Column(name = “REQUEST_DATE”, nullable = false, columnDefinition = “date default sysdate″)
share|improve this answer
I have tried it already. @Column(name = "REQUEST_DATE", columnDefinition = "DATE DEFAULT sysdate NOT NULL"). It doesn't work. –  chedine Aug 5 '10 at 9:57
@chedine: Please mention in your question what you already tried and why it doesn't work for you. See my other comment for rationale behind this. –  Pascal Thivent Aug 5 '10 at 10:01
@chedine: What error does it throw ? when you use solution similar to mine. Coz I am using this and don't face any issue. –  YoK Aug 5 '10 at 10:13
As per this, opensource.atlassian.com/projects/hibernate/browse/HHH-4341, the solution is vendor specific and unfortunately doesn't work with oracle. Not having the ability to specify the default value seems to be a missing feature. –  chedine Aug 5 '10 at 10:26
can you post what error you get with above solution. –  YoK Aug 5 '10 at 10:38

Assign a default value to the field:

private Date requestDate = new Date();
share|improve this answer
This is a web application. The above line will insert server time which will be different from sysdate (assuming the appserver and db are in diff timezones). Also the same code will be running in different development servers but all pointing to same db instance. –  chedine Aug 5 '10 at 9:53
@chedine: If you have specific constraints, why don't you add them in the question?! You would better answers and people wouldn't waste their time suggesting something that doesn't match your constraints... –  Pascal Thivent Aug 5 '10 at 10:00
May be, my mistake. Anyways, thanks for your time. –  chedine Aug 5 '10 at 10:03
I'll still leave this answer for the benefit of people with different constraints than yours. –  Bozho Aug 5 '10 at 11:59
Helped me, nice that you left it here –  TarunG Jul 2 at 6:25

Make the default in Oracle for the column SYSDATE:


Then, from Hibernate's perspective it can be nullable.

Hibernate will save a NULL to the database. Oracle will convert that to SYSDATE. And everyone will be happy.

share|improve this answer
That is how it is now. But I get an exception when i try to insert a object with requestdate=null. Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("DBINST"."APP"."REQUEST_DATE") –  chedine Aug 5 '10 at 10:09
If you run the same INSERT from SQL*Plus or something, does it work or give you that error? –  WW. Aug 5 '10 at 10:40
Same error "Cannot insert null". –  chedine Aug 5 '10 at 11:04

I resolved assigning a value to the variable like this private Integer active= 0;


public class ServiziTipologia {

     private Integer id;
     private String product;
     private String description;
     private Integer active= 0;
share|improve this answer

Your Answer


