如何使用Hibernate设置默认实体属性值 [英] How to set a default entity property value with Hibernate

查看:107
本文介绍了如何使用Hibernate设置默认实体属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在休眠"字段中设置默认值?

How do I set a default value in Hibernate field?

推荐答案

如果要使用真实的数据库默认值,请使用columnDefinition:

If you want a real database default value, use columnDefinition:

@Column(name = "myColumn", nullable = false, columnDefinition = "int default 100") 

请注意,columnDefinition中的字符串与数据库有关.同样,如果选择此选项,则必须使用dynamic-insert,因此Hibernate不会在插入时包括具有null值的列.否则,谈论默认值是无关紧要的.

Notice that the string in columnDefinition is database dependent. Also if you choose this option, you have to use dynamic-insert, so Hibernate doesn't include columns with null values on insert. Otherwise talking about default is irrelevant.

但是,如果您不希望数据库默认值,而只是Java代码中的默认值,只需像这样初始化变量-private Integer myColumn = 100;

But if you don't want database default value, but simply a default value in your Java code, just initialize your variable like that - private Integer myColumn = 100;

这篇关于如何使用Hibernate设置默认实体属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆