commit
commit

Reputation: 4807

Default value not working in hibernate

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;
}

Upvotes: 7

Views: 12358

Answers (4)

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18403

@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.

Upvotes: 9

Charitha
Charitha

Reputation: 327

Make the default values in DB side. If you want to use it in entity mapping just set default value as below

private String sourceFrom ="Case";

Upvotes: -1

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16060

Your proposed solution should work, but is database dependent, so you might want to check up on whether your syntax actually works with your concrete DB. Another more generic approach is

@PrePersist
void preInsert() {
   if ( getSourceFrom() == null ) { setSourceFrom( "Case" ); }
}

Cheers,

Upvotes: 2

m-szalik
m-szalik

Reputation: 3654

Your default value is defined in database, so Hibernate doesn't use it. The default value is set by database, but the entity you created is already in hibernate cache (it can be session cache or L2 cache).

If you load the entity it comes from Hibernate cache rather from database.

To solve this problem invoke do one of:

  • refresh the entity session.refresh(yourEntity)
  • initialize it using java instead of via default value
  • use @PrePersist listener

Upvotes: 0

Related Questions