Default Values With Hibernate Annotations

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

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



4 Responses to “Default Values With Hibernate Annotations”  

  1. 1 Okada

    Cool

    great tip

  2. 2 Mateo

    I believe this is db dependent. Currently the ability to set the default value with annotations (in a non-db-dependent way) is a to-be-implemented feature in Hibernate3.3.0 according to their jira project. See here: http://opensource.atlassian.com/projects/hibernate/browse/ANN-604

  3. 3 dave

    Thanks for the tip Mateo – I haven’t used Hibernate since i posted this, and at that time i was only using MySQL… I had no idea it was platform-dependent.

  4. 4 JR Galia

    i used this in my mapping
    <property name=“accessRole” column=“access_role” generated=“insert” />

Leave a Reply