Question

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?

Was it helpful?

Solution

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 {
    //....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top