【问题标题】:Hibernate -- @Column.unique for non-key fieldsHibernate -- @Column.unique 用于非关键字段
【发布时间】:2014-04-01 23:56:24
【问题描述】:

这是寻找验证而不是问题:

@Column.unique 的 Hibernate 检查并确保 JPA 级别的列条目的唯一性。 这里的列可以是任何列,不一定是键或组合键的一部分。

@Column.uniquenon-key 字段上工作正常 - 检查字段值,如果它已经在我期望的表中,则不插入它。 Hibernate 在注释 @Column(unique=true) 上为列添加索引——这也很好。

但是,@Column.unique specs 读起来像是关键字段。

(Optional) Whether the property is a unique key. This is a shortcut for the UniqueConstraint annotation at the table level and is useful for when the unique key constraint is only a single field. This constraint applies in addition to any constraint entailed by primary key mapping and to constraints specified at the table level. 

我想知道我是否在这里遗漏了一些东西——我以后是否会遇到麻烦。

ps:我知道使用 hibernate 的唯一约束 确保列条目的唯一性 在 JPA 级别——并且不确保它在数据库本身的下方。 这不是问题。

pps;我也知道在@Table 中使用@UniqueConstraint 属性作为替代方法。 这也不是我所追求的。它在表级别做同样的事情。

TIA。

【问题讨论】:

    标签: hibernate jpa


    【解决方案1】:

    据我所知,javax.persistence.Column 注释的unique 属性在生成 DDL 期间使用。

    这意味着,如果您尝试从映射的类创建数据库模式,那么在该属性存在并设置为 true 的任何地方,都会将唯一约束添加到数据库中的相应列。

    但是,根据 JPA 规范,它在应用程序的运行时不会产生任何影响。

    【讨论】:

      猜你喜欢
      • 2015-07-11
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      相关资源
      最近更新 更多