我正在使用Hibernate 5.4.10.Final
,并尝试创建保存在@OneToOne
中的双向@JoinTable
关系。它可以工作,但是我无法命名外键,最终无法使用Hibernate生成的MD5。
请考虑以下两个实体:Email
和Account
。他们可以在联接表@OneToOne
中具有email_account
关系,并在Email
实体内部映射(拥有):
@OneToOne(fetch = FetchType.LAZY)
@JoinTable( name = "email_account",
joinColumns = @JoinColumn( name = "email_id",
referencedColumnName = "id",
foreignKey = @ForeignKey(name = "`fk:email_account<is>email`")),
inverseJoinColumns = @JoinColumn( name = "account_id",
referencedColumnName = "id",
foreignKey = @ForeignKey(name = "`fk:email_account<of>account`")),
foreignKey = @ForeignKey(name = "`fk:email_account<is>email`"),
inverseForeignKey = @ForeignKey(name = "`fk:email_account<of>account`"))
private Account account;
@JoinTable.foreignKey
/ @JoinTable.inverseForeignKey
和@JoinTable.joinColumns.foreignKey
都不起作用(如上所述,分别对它们进行尝试并一起尝试)。
我还尝试将Hibernate的旧@ForeignKey
应用于字段:
@OneToOne(...) // same as above snippet
@JoinTable(...) // same as above snippet
@org.hibernate.annotations.ForeignKey( name = "`fk:email_account<is>email`",
inverseName = "`fk:email_account<of>account`")
private Account account;
在这种情况下,将创建fk:email_account<is>email
,但不会创建反函数。应用于拥有方(Account
)的此注释不起作用。
这是一个错误还是我做错了?还有其他解决方案吗?
编辑:
Hibernate: create table email_account (account_id bigint, email_id bigint not null, primary key (email_id)) engine=InnoDB
Hibernate: alter table email_account add constraint FKbielrkd94qhlqshaoyu5wcom0 foreign key (account_id) references account (id)
Hibernate: alter table email_account add constraint FKm0rybu0s2dqc8qvhi0562w6av foreign key (email_id) references email (id)