Java 如何命名自 JPA 2.1 以来 ManyToOne 引用的外键约束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23007356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:58:14  来源:igfitidea点击:

How to name the foreign key constraint of ManyToOne references since JPA 2.1?

javahibernatejpa

提问by membersound

@org.hibernate.annotations.ForeignKeyhas been deprecated, but I cannot find any examples how the JPA 2.1 equivalent would have to look like?

@org.hibernate.annotations.ForeignKey已被弃用,但我找不到任何示例 JPA 2.1 等价物会是什么样子?

@ManyToOne
@ForeignKey(name = "FK_USER") //@deprecated Prefer the JPA 2.1 introduced {@link javax.persistence.ForeignKey} instead.
private User user;

How is this to be implemented without the deprecated annotation?

在没有弃用注释的情况下如何实现?

采纳答案by JB Nizet

As the documentationindicates, this annotation can't be applied to anything:

正如文档所示,此注释不能应用于任何内容:

@Target(value={})

@目标(值={})

It can thus only be used as part of another annotation (listed in the See Alsosection):

因此,它只能用作另一个注释的一部分(在另请参阅部分中列出):

@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"))

回答by Blekit

You're right, I misread the documentation. It can be defined as a part of @JoinColumnannotation.

你是对的,我误读了文档。它可以定义为@JoinColumn注解的一部分。

It should look like that:

它应该是这样的:

@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"))

回答by Robert Danilo

Do you insert @JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"))direct to mapped to entity example:

您是否插入@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"))直接映射到实体示例:

@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"))
private User user;