JPA - defining multi-column unique constraints

╄→гoц情女王★ 提交于 2019-12-08 17:43:28

问题


Is it possible using JPA to define 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


回答1:


With the uniqueConstraints attribute of the Table annotation:

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


来源:https://stackoverflow.com/questions/6650589/jpa-defining-multi-column-unique-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!