How to set a default entity property value with Hibernate

后端 未结 17 2208
一整个雨季
一整个雨季 2020-11-28 02:53

How do I set a default value in Hibernate field?

相关标签:
17条回答
  • 2020-11-28 03:05

    If you want to set default value in terms of database, just set @Column( columnDefinition = "int default 1")

    But if what you intend is to set a default value in your java app you can set it on your class attribute like this: private Integer attribute = 1;

    0 讨论(0)
  • 2020-11-28 03:07

    Default entity property value

    If you want to set a default entity property value, then you can initialize the entity field using the default value.

    For instance, you can set the default createdOn entity attribute to the current time, like this:

    @Column(
        name = "created_on"
    )
    private LocalDateTime createdOn = LocalDateTime.now();
    

    Default column value using JPA

    If you are generating the DDL schema with JPA and Hibernate, although this is not recommended, you can use the columnDefinition attribute of the JPA @Column annotation, like this:

    @Column(
        name = "created_on", 
        columnDefinition = "DATETIME(6) DEFAULT CURRENT_TIMESTAMP"
    )
    @Generated(GenerationTime.INSERT)
    private LocalDateTime createdOn;
    

    The @Generated annotation is needed because we want to instruct Hibernate to reload the entity after the Persistence Context is flushed, otherwise, the database-generated value will not be synchronized with the in-memory entity state.

    Instead of using the columnDefinition, you are better off using a tool like Flyway and use DDL incremental migration scripts. That way, you will set the DEFAULT SQL clause in a script, rather than in a JPA annotation.

    For more details about using the @Generated annotation, check out this article.

    Default column value using Hibernate

    If you are using JPA with Hibernate, then you can also use the @ColumnDefault annotation, like this:

    @Column(name = "created_on")
    @ColumnDefault(value="CURRENT_TIMESTAMP")
    @Generated(GenerationTime.INSERT)
    private LocalDateTime createdOn;
    

    Default Date/Time column value using Hibernate

    If you are using JPA with Hibernate and want to set the creation timestamp, then you can use the @ColumnDefault annotation, like this:

    @Column(name = "created_on")
    @CreationTimestamp
    private LocalDateTime createdOn;
    

    For more details about using the @CreationTimestamp annotation, check out this article.

    0 讨论(0)
  • 2020-11-28 03:08

    use hibernate annotation

    @ColumnDefault("-1")
    private Long clientId;
    
    0 讨论(0)
  • 2020-11-28 03:11

    You can use @PrePersist anotation and set the default value in pre-persist stage.

    Something like that:

    //... some code
    private String myProperty;
    //... some code
    
    @PrePersist
    public void prePersist() {
        if(myProperty == null) //We set default value in case if the value is not set yet.
            myProperty = "Default value";
    }
    
    // property methods
    @Column(nullable = false) //restricting Null value on database level.
    public String getMyProperty() {
        return myProperty;
    }
    
    public void setMyProperty(String myProperty) {
        this.myProperty= myProperty;
    }
    

    This method is not depend on database type/version underneath the Hibernate. Default value is set before persisting the mapping object.

    0 讨论(0)
  • 2020-11-28 03:15

    If you want a real database default value, use columnDefinition:

    @Column(name = "myColumn", nullable = false, columnDefinition = "int default 100") 
    

    Notice that the string in columnDefinition is database dependent. Also if you choose this option, you have to use dynamic-insert, so Hibernate doesn't include columns with null values on insert. Otherwise talking about default is irrelevant.

    But if you don't want database default value, but simply a default value in your Java code, just initialize your variable like that - private Integer myColumn = 100;

    0 讨论(0)
  • 2020-11-28 03:15

    The above suggestion works, but only if the annotation is used on the getter method. If the annotations is used where the member is declared, nothing will happen.

    public String getStringValue(){
         return (this.stringValue == null) ? "Default" : stringValue;
    }
    
    0 讨论(0)
提交回复
热议问题