Jpa @OnetoOne与Java8的关系

时间:2018-08-07 10:24:28

标签: jpa java-8 optional

我不能在JPA实体中使用 java.util.Optional (jdk8 +)。

@OneToOne(optional = false, mappedBy = "authorId")
private Optional<Author> author;

我得到:

Caused by: org.hibernate.AnnotationException: Unknown mappedBy in:
com.mypackage.Album.author, referenced property unknown: java.util.Optional.authorId

1 个答案:

答案 0 :(得分:2)

如果要使用可选选项,请实施getter方法:

@OneToOne(optional = false, mappedBy = "authorId")
private Author author;

public getAuthor(){
    return Optional.ofNullable(author);
}

查看this以获得详细信息。