gpt4 book ai didi

java - 在 Hibernate 映射中 : how to set default value?

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:30 25 4
gpt4 key购买 nike

In my project i want to set default value of three columns i'm using this code in x.hbm.xml file:

<property name="occupation" type="string">
<column name="occupation" length="20" not-null="false" default="Not Provided" />
</property><property name="address" type="string">
<column name="address" length="150" not-null="false" default="Not Provided" />
</property><property name="contact" type="string">
<column name="contact" length="15" not-null="false" default="Not Provided" />
</property>

when all three values are not provided by user then only first(occupation) is set to default value others not.. or more then one field is not provided then only one column get default value other not..

最佳答案

我尝试使用注释。

@Column(name = "colName", insertable=false, updatable = false, nullable = false, 
columnDefinition = "varchar(255) default '14-APR-1981'")

或者您可以使用@PrePersist注释

@PrePersist
public void prePersist() {
if(myProperty == null) //We set default value in case if the value is not set yet.
myProperty = "Default value";
}

关于java - 在 Hibernate 映射中 : how to set default value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22934974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com