在Hibernate JPA2上使用唯一约束 [英] Using unique constraint on Hibernate JPA2

查看:179
本文介绍了在Hibernate JPA2上使用唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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上使用唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆