Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernation annotations, specify column default value

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).

like image 419
chedine Avatar asked Nov 28 '22 19:11

chedine


1 Answers

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.

like image 187
chedine Avatar answered Dec 05 '22 15:12

chedine