Article.java

@OneToMany
@JoinColumn(name = "refId", referencedColumnName = "id")
private List<Attachment> attachmentList;

Book.java

@OneToMany
@JoinColumn(name = "refId", referencedColumnName = "id")
private List<Attachment> attachmentList;

Attachment.java

private String refId

The Article and Book has many Attachment and join with same column "refId". The Attachment Entity how to code?

有帮助吗?

解决方案

It doesn't really make sense to me: How can I know, by looking at an Attachment record, whether it is a Book or an Article that the refId column is referring to? You should have separate columns to store relationship of Attachment to Book and Article.

If Book and Article are sharing the key, that implies Book and Article should share a parent class, then it should look something like:

class Document {
    @Id
    Long id;

    @OneToMany
    List<Attachment> attachments;
}

class Article extends Document {
    //....
}

class Book extends Document {
    //....
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top