gpt4 book ai didi

java - 在hibernate中定义唯一键

转载 作者:行者123 更新时间:2023-12-01 08:55:07 25 4
gpt4 key购买 nike

我的项目中有三个模型。

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

最佳答案

对于复合唯一键,您可以使用@Table注释的uniqueConstraints属性:

@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;
}

关于java - 在hibernate中定义唯一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com