【问题标题】:How to set up hbm2ddl to not allow null in certain variables / columns?如何设置 hbm2ddl 在某些变量/列中不允许为空?
【发布时间】:2013-02-06 12:57:32
【问题描述】:

我正在调试一个带有 Spring 和 hibernate 的项目。有两个实体的表由 hbm2ddl 自动创建。这大部分都可以正常工作,但架构表明除了主 ID 之外的所有列都允许为空。我想告诉 hbm2ddl 创建其中一些不允许空值的列。这是如何实现的?

【问题讨论】:

    标签: spring hibernate hbm2ddl


    【解决方案1】:

    您需要在映射中指定这些约束,无论是 hbm 文件还是注释。

    对于 hbm 文件:

    <property type="string" name="label" column="M_LABEL" not-null="true"/>
    

    对于注释:

    @Column(name = "M_LABEL", nullable = false)
    private String label;
    

    【讨论】:

    • 是的。我正朝着那个方向思考。不过,我正在使用注释,但在找到正确的注释时遇到了一些麻烦。您的提示使我走上了正轨。我需要在 @Column 注释中添加 nullable=false 。我错误地寻找 NotNull (在 Spring Validation 中使用)或类似的东西。谢谢!
    猜你喜欢
    • 2017-12-16
    • 2017-01-24
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2013-04-01
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多