Saturday, 31 August 2013

How to use same entity in different relations in JPA?

How to use same entity in different relations in JPA?

I have created an entity named Image. It holds image metadata etc that I
save in the database. Currently I have an one-to-many relationship between
Question and Image. So I have declared @OneToMany and @JoinColumn
annotation on the list of images in question entity. This means there is
an foreign key in the image table named question_id in my case.
I want to use this entity in other relationships too. User could have
images and so on. However, this would mean that I need a user_id column in
the image table too. As this grows this seems like a bad approach.
How can I do this? Do I need to use inheritance and create subclasses like
QuestionImage, UserImage and so on?
Or are there other better (and simpler ways)?

No comments:

Post a Comment