How do I generate a migration file with @Check contraints in Hibernate?

I have a problem. I put the @Check annotation with the appropriate checks over the entity field. Further, I tried to generate the migration file both through the “DDL by Entities” function and through Liquibase. However, neither of these, when generating the migration script, takes into account in any way at all the @Check-constraints I set in the entities over fields. Is there any way or tools to account for the constraints in the migration files from the @Check annotation?

Translated with DeepL.com (free version)

Upvotes: 0

Views: 28

Answers (1)

Ali İhsan TAŞDELEN
Ali İhsan TAŞDELEN

Reputation: 21

It is so easy. Firstly, let me give you a basic sample.

@Entity
@Check(constraints = "COL_AGE > 18")
public class User{

    ...

    @Column(name = "COL_AGE")
    private int age;

}

If the user is underage, database doesn't accept it. After that, you can add easily DDL to migration file.

ALTER TABLE user
ADD CONSTRAINT check_age CHECK (COL_AGE > 18);

I hope it helps you well. Take care.

Upvotes: 0

Related Questions