我可以将javax.persistence.OneToOne与javax.persistence.ForeignKey一起使用吗?

时间:2014-05-27 10:51:35

标签: hibernate jpa annotations

以下代码有什么问题?我想要实现的是确定生成的外键的名称。

@Entity
public class Artifact {
@Id private long id;
@OneToOne(optional=false, fetch=FetchType.LAZY)
@ForeignKey(name="FK_ARTI_REPO")
private Repository repository;

但编译器显示此错误消息:

The annotation @ForeignKey is disallowed for this location.

代码有什么问题?

2 个答案:

答案 0 :(得分:6)

替换

@ForeignKey(name="FK_ARTI_REPO")

通过

@JoinColumn(name = "COLUMN_NAME", foreignKey = @ForeignKey(name = "FK_ARTI_REPO"))

答案 1 :(得分:2)

如任何体面的JPA文档所示,你不能单独使用ForeignKey,而是使用JoinColumn等,所以为1-1关系指定@JoinColumn

http://www.datanucleus.org/products/accessplatform/jpa/annotations.html#ForeignKey