JPA 嵌套映射到没有基础的视图 Table

JPA Nested Mapping to View without a Base Table

所以我有一个名为 OmsJob 的视图。我将它映射到 POJO,它有 draftFiletrackedFilecleanFile 和其他与 files [=38= 链接的文件].和往常一样,我绘制了这样的图:

@Entity
@EntityListeners(PreventAnyUpdate.class)
@ConfigurationProperties("omsjob")
@Table(name = "OMSJob")
public class OmsJob {
    @NotNull
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "draft_file_id")
    private SomeFile draftFile;
}

效果很好,基本上是:

{
  "omsjob": {
    "draftFile": {}
  }
}

但我想要这样:

{
  "omsjob": {
    "fileDetails": {
      "draftFile": {}
    }
  }
}

所以我创建了一个 class 并将其命名为 FileDetails 并将其 @Embeddable@Embedded 放入 OmsJob 中,如下所示:

@Embeddable
public class FileDetails {
    @NotNull
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "draftFileId")
    private SomeFile draftFile;
}

@Entity
@EntityListeners(PreventAnyUpdate.class)
@ConfigurationProperties("omsjob")
@Table(name = "OMSJob")
public class OmsJob {
    @Embedded
    private FileDetails fileDetails;
}

我得到的错误很明显,Caused by: java.sql.SQLException: 'MySchema.OMSJob' is not BASE TABLE。它试图将外键添加到 table 并且我已将其映射到视图。但是我怎样才能实现我想要的,这是否是正确的方法?

更新 1

代码运行正常,它只是在启动应用程序时抛出错误。应该找到一种方法来处理该错误。

只需添加@javax.persistence.ForeignKey注解即可防止JPA添加外键:

@Embeddable
public class FileDetails {

    @NotNull
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "draftFileId", foreignKey = @ForeignKey(name = "none"))
    private SomeFile draftFile;

    /**
     * @return the draftFile
     */
    public SomeFile getDraftFile() {
        return draftFile;
    }
}