我尝试按如下方式设置默认数据库值,但我得到:
错误: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;
设置Date
和long
的默认值的正确方法是什么?
答案 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;