最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How do I generate a migration file with @Check contraints in Hibernate? - Stack Overflow

programmeradmin5浏览0评论

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 (free version)

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 (free version)

Share Improve this question asked 13 hours ago Дмитрий ДмитриевДмитрий Дмитриев 311 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论