在多个引用列上添加唯一约束

时间:2013-11-25 13:28:22

标签: hibernate postgresql jpa unique-constraint

您好我想在两个引用的列上添加唯一约束:

@Entity()
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "ast", "tfs" }))
public class N extends UUID {

@ManyToOne(optional = false)
@JoinColumn(name = "ast")
private A ast;

@ManyToOne(optional = false)
@JoinColumn(name = "tfs")
private T tfs;

类UUID是用于生成主ID的逻辑。

Hibernate生成3个Constraint,PK,2个Secondary Keys ..但我期待4个约束。 有人有想法吗?

编辑:

@Entity
public class A extends UUID implements Serializable {

@OneToMany(mappedBy = "ast")
private List<N> nutz;

@Entity
public class T extends UUID implements Serializable {

@OneToMany(mappedBy = "tfs")
private List<N> nutz;

1 个答案:

答案 0 :(得分:1)

如果我设置:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

而不是

<property name="hibernate.hbm2ddl.auto" value="update" />

似乎Hibernate只在创建整个数据库时才创建constaints而不是在创建表时创建constaints!混乱。 related