postgresql 多列的唯一约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11955952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:40:37  来源:igfitidea点击:

Unique Constraint Over Multiple Columns

hibernatepostgresqlseamhibernate-mappingseam2

提问by DaveB

I am using SEAM 2/Hibernate along with PostgreSQL 9 database. I have the following table

我正在使用 SEAM 2/Hibernate 和 PostgreSQL 9 数据库。我有下表

Active Band
===========
active_band_id serial
active_band_user text
active_band_date timestamp
active_band_process integer

I would like to add a constraint that ensures each new entry has a unique combination of active_band_user and active_band_date.

我想添加一个约束,以确保每个新条目都具有 active_band_user 和 active_band_date 的唯一组合。

There could potentially be many attempted inserts per second so I need this to be as efficient as possible, is there a SEAM / hibernate annotation I can use in the entity mapping?

每秒可能有很多尝试插入,所以我需要尽可能高效,是否有可以在实体映射中使用的 SEAM/hibernate 注释?

Thanks in advance

提前致谢

回答by Mikko Maunu

There is no Hibernate annotation that checks uniqueness before insert/update. But there is annotation which will generate such a constraint to database if automatic database creation is used:

没有在插入/更新之前检查唯一性的 Hibernate 注释。但是如果使用自动数据库创建,则有注释会为数据库生成这样的约束:

 @Table(
    name="ACTIVE_BAND", 
    uniqueConstraints=
        @UniqueConstraint(columnNames={"active_band_user", "active_band_date"})
)