Dave Perrett

Default Values With Hibernate Annotations

database, hibernate, java, programming, quickie, web

To set the default value of a field with Hibernate, you can use the columnDefinition argument :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * Returns the file size of this Photo
 */
@Column(name="file_size",nullable=false,columnDefinition="bigint(20) default 0")
public Long getFileSize() {
    return fileSize;
}

/**
 * Sets the file size of this Photo
 *
 * @param fileSize File size of this Photo
 */
public void setFileSize(Long fileSize) {
    this.fileSize = fileSize;
}