问题描述
如何在hibernate POJO上实现我的唯一约束?假设数据库不包含任何内容。
我已经看到 @Column()
注释中的唯一属性,但是我无法使它工作?
如果我想将此约束应用到多个列上,该怎么办?
如何在hibernate POJO上实现我的唯一约束?假设数据库不包含任何内容。
我已经看到 @Column()
注释中的唯一属性,但是我无法使它工作?
如果我想将此约束应用到多个列上,该怎么办?
基本上,你不能实施无数据库支持的独特约束。 $ b
@UniqueConstraint
和独特
属性<$ c
你可以做在插入新实体之前进行某种手动检查,但在这种情况下,您应该了解并发事务的可能问题。
因此,在数据库中应用约束是首选。
How can I implement my unique constraints on the hibernate POJO's? assuming the database doesn't contain any.
I have seen the unique attribute in @Column()
annotation but I couldn't get it to work?
What if I want to apply this constraint to more than one column?
Bascially, you cannot implement unique constraint without database support.
@UniqueConstraint
and unique
attribute of @Column
are instructions for schema generation tool to generate the corresponsing constraints, they don't implement constraints itself.
You can do some kind of manual checking before inserting new entities, but in this case you should be aware of possible problems with concurrent transactions.
Therefore applying constraints in the database is the preferred choice.
这篇关于在Hibernate JPA2上使用唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!