How to get a unique key for two fields with Hibernate?

后端 未结 2 1495
南笙
南笙 2020-12-23 18:24

I have two fields of an entity class which I don\'t want to be unique but to instead be used as composite fields for a key which must itself be unique. For example I have t

2条回答
  •  臣服心动
    2020-12-23 19:17

    This will create a unique key on the database:

    @Table( name = "MYTABLE",
            uniqueConstraints = { @UniqueConstraint( columnNames = { "NAME", "VERSION" } ) } )
    

    This will be enforced by the database on a update or persist.

    You'd need to write your own custom validator if you wanted to enforce this using Hibernate Validator.

提交回复
热议问题