문제

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