Java 在 Hibernate 中使用注释定义默认列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28107554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define default column value with annotations in Hibernate
提问by Jardo
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.
我知道在 SO 和网上有很多这样的问题,但所有的答案都建议使用columnDefinition
哪个是特定于数据库的,因此不适用于我,因为我正在使用的系统需要在不同的数据库上运行。
I found this hibernate issuewhere someone requested this feature for annotations. The issue has been closed saying that another issuewill 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.
我发现了这个休眠问题,其中有人要求使用此功能进行注释。该问题已关闭,表示另一个问题将涵盖该功能。第二个问题显然添加了注释@Generated
以及其他一些注释,但我找不到有关如何使用这些新注释定义默认列值的任何文档。
So my question is: Does anyone know how can I define a default column value with annotations (and NOT using columnDefinition
)?
所以我的问题是:有谁知道如何使用注释(而不是使用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.
编辑:进一步澄清我的问题:当我添加一个新的非空列时,我需要 Hibernate 来更新现有架构(将新列添加到相应的表中)。但是由于该列是非空的,数据库不能在没有指定默认值的情况下创建该列(如果表中已经有一些行)。所以我需要指示 Hibernate 发出以下 DDL 语句:ALTER TABLE my_table ADD COLUMN new_column VARCHAR(3) DEFAULT 'def'
,但它必须独立于使用的数据库。
采纳答案by Babl
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.
我认为您不需要任何文档,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 "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
}
回答by ArunDhwaj IIITH
Following is working for me.
以下是为我工作。
@ColumnDefault("'0.0'")
@ColumnDefault("'0.0'")
@Column(name = "avgRating")
@Column(name = "avgRating")
private float avgRating;
私人浮动平均评级;