我的项目中有三个模型。
public class Bank{
private int id;
private String title;
....
}
public class Branch{
private Bank bank;
private int id;
private String title;
....
}
public class Account{
private Branch branch;
private int id;
private String accountNumber;
private String title;
....
}
如何定义accountNumber和id of bank合并为唯一键?
答案 0 :(得分:1)
对于复合唯一键,您可以使用uniqueConstraints
注释的@Table
属性:
@Table(
name = "account",
uniqueConstraints = {@UniqueConstraint(columnNames = {"accountNumber", "bank"})}
)
public class Account{
private Branch branch;
private int id;
private String accountNumber;
private String title;
@ManyToOne
private Bank bank;
}