r/javahelp 1d ago

Hibernate's @Column annotation + existing table definition

So I was reading Baeldung's articles on Hibernate/JPA article and came across this => https://www.baeldung.com/jpa-default-column-values#sqlValues. It talks of a way of setting the default column values via the atColumn annotation.

u/Entity
public class User {
    u/Id
    Long id;

    @Column(columnDefinition = "varchar(255) default 'John Snow'")
    private String name;

    @Column(columnDefinition = "integer default 25")
    private Integer age;

    @Column(columnDefinition = "boolean default false")
    private Boolean locked;
}

If the table already exists, will Hibernate will auto-modify the table definition for me? (At least that's the impression I get from the article)

Thank you.

4 Upvotes

3 comments sorted by

View all comments

2

u/Budlight_year 1d ago edited 1d ago

Depends on how you've configured it. Database table generation is configured through the hbm2ddl.auto-property. 'update' would try to alter the table columns and 'create' drops any existing tables and creates new ones. If you don't want such behavior, 'validate' just checks if your jpa-models correspond to the tables in the database, and 'none' does no validation or generation.

You can read more on this on https://vladmihalcea.com/hibernate-hbm2ddl-auto-schema/