如何设置默认数据库值

时间:2014-12-21 22:31:20

标签: java mysql hibernate jpa hibernate-mapping

我尝试按如下方式设置默认数据库值,但我得到:

错误:Caused by: java.lang.NullPointerException表示:

@Column(name = "dateOfBirth", insertable = false, updatable = false, nullable=false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dateOfBirth;

和...

错误:Caused by: java.lang.NumberFormatException: For input string: ""表示:

@Column(name = "zip", insertable = false, updatable = false, nullable=false, columnDefinition = "bigint(20) default 0")
private long zip;

设置Datelong的默认值的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

在这两种情况下,您都需要删除此属性:

nullable=true

当DB获得NULL值时,无论如何都将应用DEFAULT。

答案 1 :(得分:0)

如何使用Java代码中的默认值初始化属性:

@Column(name = "dateOfBirth", insertable = false, updatable = false, nullable=false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dateOfBirth = new Date(); // or however you like to initialize this value.

@Column(name = "zip", insertable = false, updatable = false, nullable=false, columnDefinition = "bigint(20) default 0")
private long zip = 1001L;