Hibernate -- @Column.unique for non-key fields



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

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

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

但是,@Column.unique规格读起来就像关键字段一样。

(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:我知道使用休眠的唯一约束确保列条目的唯一性在 JPA 级别 - 并且不能确保它在数据库本身的下方。这不是问题。

PPS; 我也知道在@Table内使用@UniqueConstraint财产作为替代方案。那也不是我所追求的。它在表级别做同样的事情。

蒂亚。

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

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

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

相关内容

最新更新