Creating a composite Unique constraints on multiple columns

前端 未结 3 1729
轻奢々
轻奢々 2021-02-01 00:33

This is my model:

class User {...}
class Book {
  User author;
  int number;
}

Every book number starts at 1 per author and increments upwards.

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 00:59

    Use @UniqueConstraint:

    @Table(
        uniqueConstraints=
            @UniqueConstraint(columnNames={"author_id", "number"})
    )
    @Entity
    class Book extends Model {
       @ManyToOne
       @JoinColumn(name = "author_id")
       User author;
       int number; 
    } 
    

提交回复
热议问题