Java – hibernate comment, specifying column defaults

I have a domain object with the following comments

@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
*/
 }

Request_ The date column cannot be null. According to DDL, the default value is sysdate (Oracle dB) How to annotate this field so that when the requestdate property is null, hibernate will automatically insert sysdate At present, when the field is empty, it will throw an error, which is very obvious because it cannot be empty according to database constraints What shall I do? Another method is to mark this field as transient and insert it to work normally On the negative side, I will not be able to retrieve the value (of the request_date column)

Solution

You can put the default value in column definition An example is as follows:

@Column(name = "REQUEST_DATE",nullable = false,columnDeFinition = "date default sysdate")
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>