@ ForeignKey#name无法与@JoinTable @OneToOne映射一起使用?

时间:2019-12-22 18:19:57

标签: hibernate

我正在使用Hibernate 5.4.10.Final,并尝试创建保存在@OneToOne中的双向@JoinTable关系。它可以工作,但是我无法命名外键,最终无法使用Hibernate生成的MD5。

请考虑以下两个实体:EmailAccount。他们可以在联接表@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)

0 个答案:

没有答案