Can I do this in JPA/Hibernate/Spring?
I have a simple hierarchy of objects which starts from the QuizResponse
object.
First two of these are important:
UserPersonalityQuizResponse
extends PersonalityQuizResponse开发者_如何学JAVA extends QuizResponse
UserScoredQuizResponse
extends ScoredQuizResponse extends QuizResponse
...
AnonymousPersonalityQuizResponse
extends PersonalityQuizResponse extends QuizResponse
AnonymousScoredQuizResponse
extends ScoredQuizResponse extends QuizResponse
These two, UserPersonalityQuizResponse
and UserScoredQuizResponse
, have a "user" property mapped on a different column for each.
Is it possible to make something in JPA/Hibernate, like an interface, UserResponse
, to allow me to implement it and then to do this?
List<UserResponse> quizResponses = user.getUserQuizResponses()
Right now I need to make two queries to get this (on to retrieve userPersonalityQuizResponses
and one for UserScoredQuizResponses
).
Unfortunately the hierarchy of the QuizResponse
class does not allow UserPersonalityQuizResponse
and UserScoredQuizResponse
to extend the same parent class.
You can do a HQL query like from QuizResponse q where q.class = UserScoredQuizResponse or q.class = UserPersonalityQuizResponse
See more details about polymorphic queries here: http://docs.jboss.org/hibernate/core/3.5/reference/en/html_single/#queryhql-polymorphism
精彩评论