2012-08-14 175 views
23

我使用SEAM 2/Hibernate和PostgreSQL 9数据库。我有以下表多列唯一约束

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

我想补充一点,确保每个新条目active_band_user和active_band_date的独特组合的约束。

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

在此先感谢

+0

这似乎复制http://stackoverflow.com/questions/3211972/specifying-restrictions-unique-together-in -hibernate – 2017-04-19 14:52:43

回答

52

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

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

我不认为它支持这个,因为我在'Eclipse中注释属性Table.uniqueConstraints的值必须是一些@ javax.persistence.UniqueConstraint注释'。此方法是否在SQL模式中生成约束? – DaveB 2012-08-14 17:24:28

+0

是的,它支持。它记录在这里:http://docs.oracle.com/javaee/6/api/javax/persistence/Table.html#uniqueConstraints%28%29其他的东西一定是错的。是否导入了javax.persistence.UniqueConstraint? – 2012-08-20 10:56:17

+12

我知道这是几年前,但确保你使用'javax.persistence.Table'而不是'org.hibernate.annotations.Table'。 Hibernate的'@ Table'没有'uniqueConstraints'字段。 – Patrick 2014-02-05 18:21:00