在休眠中定义唯一键

时间:2017-02-07 06:07:44

标签: java hibernate unique-key

我的项目中有三个模型。

 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合并为唯一键?

1 个答案:

答案 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;
}