在使用JPA時(shí),如果需要為屬性設(shè)置默認(rèn)值,很自然的,你可能會(huì)想到用下面的方式。

@Column(name="state",columnDefinition="tinyint default 0")
private Integer state=0;

但很不幸的是,這種方案有時(shí)候并不可行,原因在于columnDefinition是在創(chuàng)建表的時(shí)候使用的,如果你的表不是自動(dòng)生成的。

那我們又需要為屬性添加默認(rèn)值,該怎么辦呢,其實(shí)很簡(jiǎn)單,就直接為屬性賦值就可以了。

private Integer state=0;