开发者

JPA - defining multi-column unique constraints

Is it possible using JPA to define开发者_StackOverflow社区 multiple unique constraints.

@Entity
class Foo {
    long id;

     String name;

     MyEnum type;

}

Foo.id should be unique as should combinations of {Foo.name, Foo.type}.

Ex.

id, name, type

1, "name1", "type1"

2, "name1", "type2"

3, "name1", "type1" // error duplicate of id = 1

How do I achieve this using JPA annotations?

Thanks


With the uniqueConstraints attribute of the Table annotation:

@Table(name = "FOO", uniqueConstraints={
    @UniqueConstraint(columnNames = {"NAME", "TYPE"})
})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜