JPA:@OneToOne 使用随机生成的名称创建隐式唯一约束。有什么方法可以使用注释在代码中指定该名称吗?

JPA: @OneToOne creates an implicit unique constraint with a randomly generated name. Is there any way to spcify that name in code using annotations?

在Javaclass中,我有这个属性

@OneToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private MyPropertyClass myProperty;

这不仅创建了外键约束,还创建了唯一约束。这是 Hibernate DDL 模式输出:

Hibernate: create table [...]
Hibernate: alter table [...] add constraint UK_nk1hqjkd7ln1f9a2pju1yy8ay  unique (myProperty)

是否有任何方法来指定唯一约束的名称?

请在class级别尝试@Table注解的uniqueConstraints属性。

@Table( name ="tablename " , uniqueConstraints={
       @UniqueConstraint(name="myconst", columnNames={"myProp"})
   })

public class MyEntity {

@OneToOne(fetch = FetchType.EAGER, cascade =     CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="myProp")
private MyPropertyClass myProperty;

}