Reputation: 1238
I have 4 Entities: Post, SimplePost, VotingPost, Photo in this relationships
And I would like to implement it with JPA.
Entities definitions:
abstract class Post{ /* some variable */ }
class SimplePost extends Post {
@OneToOne
private Photo photo;
}
class VotingPost extends Post {
@OneToMany
private List<Photo> photos;
}
class Photo {
//which annotation should I use here ????
//@ManyToOne or
//@OneToOne
private Post post;
}
But I have a problem in there, because between SimplePost and Photo there is a OneToOne relantionship, and between VotingPost and Photo there is a OneToMany relantionship, so I don't know which annotation should I use in the photo class on the post variable.
I know I could implement it only with VotingPost containing a List of Photos (so SimplePost would be a subset of VotingPost), but this problem is interesting and I would like to know how it's to possible to implement.
Thank you for your help.
Upvotes: 0
Views: 32
Reputation: 103135
The relationship is actually between Photo and Post. So you need to decide if a Post has one photo or many photos. So the answer is as it is it cannot be implemented.
Another option is to make a uni-directional relationship. That is, the Posts may know about the Photos but the Photo does not know about the Posts. So if your application can be designed so that the SimplePost or VotingPost owns the relationship and will manage the relationship this may work. However, given a Photo you will not be able to tell which Post it belongs to without an additional query.
Upvotes: 1