This is my model:
class User {...} class Book { User author; int number; }
Every book number starts at 1 per author and increments upwards.
Use @UniqueConstraint:
@Table( uniqueConstraints= @UniqueConstraint(columnNames={"author_id", "number"}) ) @Entity class Book extends Model { @ManyToOne @JoinColumn(name = "author_id") User author; int number; }