默认值不工作在休眠 [英] Default value not working in hibernate

查看:71
本文介绍了默认值不工作在休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 columnDefinition 来为列指定默认值,但它不存储它仅存储空值的默认值,



请帮忙,下面是我的代码

  private String sourceFrom; 
@Column(name =SourceFrom,columnDefinition =varchar(15)default'Case')
public String getSourceFrom(){
return sourceFrom;
}

public void setSourceFrom(String sourceFrom){
this.sourceFrom = sourceFrom;


解决方案

在DDL阶段使用@ Column.columnDefinition 来创建你的表,而不是在正常程序运行期间;可能你必须使用 @ DynamicInsert / @ DynamicUpdate :这个注释只插入(或更新)你设置到POJO中的属性,并让RDBMS管理其他字段。
一个小例子

  @Entity 
class MyTable {
@ Id
private int code;
@Column(name =SourceFrom,columnDefinition =varchar(15)default'Case')
private String sourceFrom;
}

这是从DDL阶段生成的代码

  create table mytable(code integer not null,SourceFrom varchar(15)default'Case')






  MyTable t = new MyTable(); 
t.setCode(10);
session.save(t);

会执行此语句

pre > insert into mytable(code,SourceFrom)values(10,NULL)






  MyTable t = new MyTable(); 
t.setCode(10);
t.setSourceFrom(MANUAL INSERT);
session.save(t);

会执行此语句

pre > insert into mytable(code,SourceFrom)values(10,'MANUAL INSERT')






如果使用 @DynamicInsert 注释 MyTable ,第一个示例会生成这样的语句:

pre $ insert into mytable(code)values(10)

您可以看到字段 SourceFrom 的值未指定,并且已插入到数据库表中的值已定义通过列定义的默认值('Case'在本例中)。 @PrePersist 或其他解决方案)仍然有效。


I have use columnDefinition to specify default value for a column, however it does not store default value it stores null only,

Please help regarding this, below is my code

private String sourceFrom;
@Column(name = "SourceFrom", columnDefinition = "varchar(15) default 'Case'")
public String getSourceFrom() {
    return sourceFrom;
}

public void setSourceFrom(String sourceFrom) {
    this.sourceFrom = sourceFrom;
}

解决方案

@Column.columnDefinition is used during DDL phase to create your table and NOT during normal program running; probably you have to work with @DynamicInsert/@DynamicUpdate: this annotations insert (or update) only properties you setted into you POJO and let RDBMS to manage other fields.
A small example

@Entity
class MyTable {
  @Id
  private int code;
  @Column(name = "SourceFrom", columnDefinition = "varchar(15) default 'Case'")
  private String sourceFrom; 
}

and this is the generated code from DDL phase

create table mytable (code integer not null,SourceFrom varchar(15) default 'Case')


MyTable t = new MyTable();
t.setCode(10);
session.save(t);

will do this statement

insert into mytable (code, SourceFrom) values (10,NULL)


MyTable t = new MyTable();
t.setCode(10);
t.setSourceFrom("MANUAL INSERT");
session.save(t);

will do this statement

insert into mytable (code, SourceFrom) values (10,'MANUAL INSERT')


If you annotate MyTable with @DynamicInsert the first example will generate this statement

insert into mytable (code) values (10)

As you can see the value of field SourceFrom is not specified and value inserted into database table is defined by column definition default value ('Case' in this case).

Manual processing of default value (in setter, with @PrePersist or other solution) are still valid.

这篇关于默认值不工作在休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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