Java 休眠注解,指定列默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3413487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:05:55  来源:igfitidea点击:

Hibernation annotations, specify column default value

javaoraclehibernateormjpa

提问by chedine

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

Request_date列不能为空,并且根据 DDL,默认值为 sysdate(oracle DB)。如何注释此字段,以便如果 requestDate 属性为 null,hiberanate 会自动插入 sysdate。?目前当字段为空时它会抛出错误,这是非常明显的,因为根据数据库约束它不能为空。我该怎么做?一种替代方法是将此字段标记为瞬态,并且插入工作正常。但不利的一面是,我将无法检索(request_date 列的)值。

采纳答案by chedine

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.

这是休眠注释中缺少的功能。Yok 已经发布了一些解决方法。问题是解决方法取决于供应商,可能不适用于所有数据库。就我而言,Oracle 无法正常工作,并已报告为错误。

回答by Bozho

Assign a default value to the field:

为该字段分配一个默认值:

private Date requestDate = new Date();

回答by YoK

You can put the default value in a columnDefinition. An example would look like:

您可以将默认值放在 columnDefinition 中。一个例子看起来像:

@Column(name = "REQUEST_DATE", nullable = false, columnDefinition = "date default sysdate")

回答by WW.

Make the default in Oracle for the column SYSDATE:

在 Oracle 中为列 SYSDATE 设置默认值:

ALTER TABLE APP MODIFY (REQUEST_DATE DEFAULT SYSDATE);

ALTER TABLE APP MODIFY (REQUEST_DATE DEFAULT SYSDATE);

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

然后,从 Hibernate 的角度来看,它可以为空。

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

Hibernate 会将 NULL 保存到数据库中。Oracle 会将其转换为 SYSDATE。而且每个人都会很开心。

回答by SaganTheBest

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

我解决了为变量赋值,如private Integer active= 0;

@Entity
@Table(name="products")

public class ServiziTipologia {

     @Id
     @GeneratedValue
     private Integer id;
     private String product;
     private String description;
     private Integer active= 0;

回答by searching9x

Using @ColumnDefault (Work for DDL update). hibernate.hbm2ddl.auto=update

使用@ColumnDefault(适用于 DDL 更新)。hibernate.hbm2ddl.auto=更新

import org.hibernate.annotations.ColumnDefault;

....

@ColumnDefault(value="'@'")
@Column(name = "TEMP_COLUMN", nullable = false)
public String getTempColumn() {
    return tempColumn;
}

DDL Generate:

DDL 生成:

Alter Table YOUR_TABLE add TEMP_COLUMN varchar2(255) default '@' not null;

回答by ponder275

If you mark your entity with @DynamicInserte.g.

如果你用@DynamicInsert例如标记你的实体

@Entity
@DynamicInsert
@Table(name = "TABLE_NAME")
public class ClassName implements Serializable  {

Hibernate will generate SQL without nullvalues. Then the database will insert its own default value. This does have performance implications See Dynamic Insert.

Hibernate 将生成没有null值的SQL 。然后数据库将插入它自己的默认值。这确实有性能影响请参阅动态插入