多列的唯一约束

Dav*_*veB 40 postgresql hibernate seam hibernate-mapping seam2

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

Active Band
===========
active_band_id serial
active_band_user text
active_band_date timestamp
active_band_process integer
Run Code Online (Sandbox Code Playgroud)

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

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

提前致谢

Mik*_*unu 79

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

 @Table(
    name="ACTIVE_BAND", 
    uniqueConstraints=
        @UniqueConstraint(columnNames={"active_band_user", "active_band_date"})
)
Run Code Online (Sandbox Code Playgroud)

  • 我知道这是多年前的事了,但请确保你使用的是`javax.persistence.Table`而不是`org.hibernate.annotations.Table`.Hibernate的`@ Table`没有`uniqueConstraints`字段. (18认同)