So I have this view called OmsJob. I'm mappint it to a POJO and it has draftFile
, trackedFile
, cleanFile
, and other files linked with the files table. And as usual, I mapped those like this:
@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;
}
which works fine and is basically:
{
"omsjob": {
"draftFile": {}
}
}
But I want it like:
{
"omsjob": {
"fileDetails": {
"draftFile": {}
}
}
}
So I created a class and named it FileDetails and made it @Embeddable
and @Embedded
it into OmsJob like this:
@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;
}
The error I got is obvious, Caused by: java.sql.SQLException: 'MySchema.OMSJob' is not BASE TABLE
. It was trying to add foreign keys to the table and I've mapped it to the view. But how can I achieve what I'm trying to and is this even the right way?
The code is working, it just throws the error while starting the application. Should find a way to handle that error.
Just need to add @javax.persistence.ForeignKey
annotation to prevent JPA from adding a foreign key:
@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;
}
}