Hibernate Inheritance Annotation clarification
In our legacy code, I stumbled upon a hibernate class implementation which I was not able to fully understand.
There is one super class corresponding to table WORK_REQUEST_GROUPS -
@Entity
@Table(name="WORK_REQUEST_GROUPS")
@Inheritance(strategy=InheritanceType.JOINED)
public class CCSRequestGroup
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="REQUEST_GROUP_ID")
private Long groupId;
....
}
Then we have a sub class corresponding to table RELEASE_CANDIDATES -
@Entity
@Table(name="RELEASE_CANDIDATES")
@PrimaryKeyJoinColumn(name="RELEASE_CANDIDATE_ID")
public class CCSReleaseCandidate extends CCSRequestGroup {
@ManyToOne
@JoinCol开发者_StackOverflowumn(name="GROUP_CONDITION_CODE")
private CCSRequestGroupCondition condition;
....
}
which according to my understanding states that CCSReleaseCandidates is the subclass of CCSRequestGroup and is joined at the column RELEASE_CANDIDATES_ID.
Now we have a method, in which we try to find out instances of CCSRequestGroup with some criteria -
List<CCSRequestGroup> requests = session.createQuery("from CCSRequestGroup requestGroup where requestGroup.condition = \'AwaitingStartTime\' order by RAND()").list();
which probably is trying to find out all the CCSRequestGroups in the current Hibernate session which have their condition = 'AwaitingStartTime'.
What is eating up my brain here is that 'condition' is a member of CCSReleaseCandidates and not CCSRequestGroup, then how are we able to get the condition member of an instance of CCSRequestGroup, successfully.
Can somebody please help me?
I imagine if you turn on SQL logging you'll see that what you think is a simple statement is actually doing an outer join with the subclass table(s) behind the scenes. That is why the data is available to Hibernate, and the HQL itself is probably taking advantage of implicit polymorphism to return the correct results to you.
精彩评论