在Hibernate中用注释定义默认列值 [英] Define default column value with annotations in Hibernate

查看:99
本文介绍了在Hibernate中用注释定义默认列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多这样的问题,并且也在网上,但所有的答案都建议使用 columnDefinition ,这是数据库特定的,因此不适用于我因为我正在处理的系统需要在不同的数据库上运行。



我发现这个 hibernate问题,有人请求此功能进行注释。该问题已被封闭,称另一个问题将涵盖该功能。第二个问题显然增加了注释 @Generated 以及其他一些问题,但我找不到任何有关如何使用这些新注释定义默认列值的文档。



所以我的问题是:有谁知道我该如何定义带注释的默认列值(而不是使用 columnDefinition )?为了进一步阐明我的问题:当我添加一个新的非空列时,我需要Hibernate来更新现有的模式(添加新列到相应的表格)。但由于该列非空,因此数据库无法在未指定缺省值的情况下创建列(如果表中已有一些行)。所以我需要指示Hibernate发出以下DDL语句: ALTER TABLE my_table ADD COLUMN new_column VARCHAR(3)DEFAULT'def',但它必须独立于使用的数据库。

解决方案

我不认为你需要任何文档,java文档是自我解释的。
如果我的理解正确,您需要一种方法来为字段设置默认值。如果是,请参阅以下代码片段。

  @Entity 
@Table(name =my_entity)
public class SomeEntity extends BaseEntity {

public static final class MyValueGenerator implements
ValueGenerator< String> {
@Override
public String generateValue(Session session,Object owner){
return这是我的默认名字;




$ b @Column(name =name,insertable = true,updatable = true,nullable = false,length = 255)
//这将添加一个DDL默认
@ColumnDefault('这是我的默认名称')
//这将添加运行时默认值。
@GeneratorType(type = MyValueGenerator.class)
私有字符串名称;

// getters and setters

}


I know there are plenty of these questions here on SO and also on the net, but all the answers suggest using columnDefinition which is database specific and hence not applicable for me because the system I'm working on needs to run on different databases.

I found this hibernate issue where someone requested this feature for annotations. The issue has been closed saying that another issue will cover that functionality. The second issue apparently added annotation @Generated and also some others, but I couldn't find any documentation on how to define the default column value with those new annotations.

So my question is: Does anyone know how can I define a default column value with annotations (and NOT using columnDefinition)?

Edit: to further clarify my problem: When I add a new not null column, I need Hibernate to update the existing schema (add the new column to the respective table). But since the column is non null, the database cannot create the column without specifying the default value (if there are already some rows in the table). So I need to instruct Hibernate to issue the following DDL statement: ALTER TABLE my_table ADD COLUMN new_column VARCHAR(3) DEFAULT 'def', but it has to be independent of the used database.

解决方案

I don't think you need any documentation, the java docs are self explaining. If I understand you correctly you need a way to set a default value for a field. If yes please see the following code snippet.

@Entity
@Table(name = "my_entity")
public class SomeEntity extends BaseEntity {

public static final class MyValueGenerator implements
        ValueGenerator<String> {
    @Override
    public String generateValue(Session session, Object owner) {
        return "This is my default name";
    }
}

@Basic
@Column(name = "name", insertable = true, updatable = true, nullable = false, length = 255)
// This will add a DDL default
@ColumnDefault("'This is my default name'")
// This will add a runtime default.
@GeneratorType(type = MyValueGenerator.class)
private String name;

// getters and setters

}

这篇关于在Hibernate中用注释定义默认列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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