【问题标题】:Unique Constraint Over Multiple Columns多列的唯一约束
【发布时间】:2012-08-10 23:02:43
【问题描述】:

我正在使用 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 注释?

提前致谢

【问题讨论】:

标签: hibernate postgresql seam hibernate-mapping seam2


【解决方案1】:

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

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

【讨论】:

  • 我不认为它支持这一点,因为我在 Eclipse 中得到“注释属性 Table.uniqueConstraints 的值必须是一些 @javax.persistence.UniqueConstraint 注释”。此方法是否在 SQL 模式中生成约束?
  • 是的,它确实支持。它记录在这里:docs.oracle.com/javaee/6/api/javax/persistence/… 肯定有其他问题。是否导入了 javax.persistence.UniqueConstraint?
  • 我知道这是几年前的事了,但请确保您使用的是 javax.persistence.Table 而不是 org.hibernate.annotations.Table。 Hibernate 的@Table 没有uniqueConstraints 字段。
【解决方案2】:

在更现代的语法中,它是:

@Table(
    name="ACTIVE_BAND", 
    uniqueConstraints =
        [UniqueConstraint(
                columnNames = ["active_band_user", "active_band_date"]
        )]
)

【讨论】:

    猜你喜欢
    • 2010-12-22
    • 2016-08-22
    • 2013-08-23
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多