是否可以使用 JPA 定义多个唯一约束。
@Entity
class Foo {
long id;
String name;
MyEnum type;
}
Foo.id 应该是唯一的,{Foo.name, Foo.type} 的组合也应该是唯一的。
例如
id, 名字, 类型
1、“name1”、“type1”
2、“name1”、“type2”
3, "name1", "type1"//id = 1 的错误重复
如何使用 JPA 注释实现此目的?
谢谢
最佳答案
使用 Table
注解的 uniqueConstraints
属性:
@Table(name = "FOO", uniqueConstraints={
@UniqueConstraint(columnNames = {"NAME", "TYPE"})
})
关于java - JPA - 定义多列唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6650589/