views:

45

answers:

4

I have a domain object and annotated as follows

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

  /**
   * Unique id for this request
   */
@Id
@GeneratedValue
@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).

A: 

Assign a default value to the field:

private Date requestDate = new Date();
Bozho
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
@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
May be, my mistake. Anyways, thanks for your time.
chedine
I'll still leave this answer for the benefit of people with different constraints than yours.
Bozho
+1  A: 

you can put default value in columnDefination. Something similar to

   @Column(name = “REQUEST_DATE”, nullable = false, columnDefinition = “date default sysdate″)
YoK
I have tried it already. @Column(name = "REQUEST_DATE", columnDefinition = "DATE DEFAULT sysdate NOT NULL"). It doesn't work.
chedine
@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
@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
As per this, http://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
can you post what error you get with above solution.
YoK
Please refer my comment to @WW post
chedine
Can you drop existing table and retry and check Table definition created in your database. Does it have "default sysdate" for date column.
YoK
Yes it has "default sysdate". The solution is not gonna work with Oracle.It works fine with MYSQL.
chedine
Just try removing "NOT NULL". And check does it function as intended, i.e. put sysdate if nothing is passed.Another thing I would like to see is, what query is being fired ? You can run hibernate in debug mode with show query option.
YoK
A: 

Make the default in oracle for the column SYSDATE:

ALTR TABLE APP MODIFY (REQUEST_DATE DEFAULT SYSDATE);

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

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

WW
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
If you run the same INSERT from SQL*Plus or something, does it work or give you that error?
WW
Same error "Cannot insert null".
chedine
Updated answer. Can't put in proper syntax because corporate firewall thinks I'm trying to do a SQL injection attack on stackoverflow. Maybe someone can edit ALTR for me.
WW
A: 

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.

chedine