这是我的模型:
class User {...} class Book { User author; int number; }
每个书的编号从每位作者的1开始并向上递增。因此,我们将有John Grisham撰写的1,2,3本书,George Martin撰写的1..5本书,等等。
我是否可以设置一个唯一的约束条件Book,以确保我们不会有两本书的作者相同?与相似@Column(unique = true),但约束仅适用于Author X number?的复合
Book
@Column(unique = true)
Author X number
用途@UniqueConstraint:
@UniqueConstraint
@Table( uniqueConstraints= @UniqueConstraint(columnNames={"author_id", "number"}) ) @Entity class Book extends Model { @ManyToOne @JoinColumn(name = "author_id") User author; int number; }