Guys, thanks a lot for answers.
Now I am having this realistic scenario and i am stuck with how i can use HQL to query what I want..
I have 2 classes, Person.java and Role. java (A person can possess multiple roles, e.g. a person can be a student, a researcher, an employee at the same time...)
Role.java class is a simple class to define a role, such as a student, a researcher, an employee, etc. No more than that.
Inside Person.java class, I am using a Set to contain all the roles this particular person can possess...
By using hibernate, I now have a table (in my Mysql database) called Person which is mapped from Person.java class, another table called Role which is mapped from Role.java class.
To demonstrate the mapping between a person and the roles he can have, I now also have a table called person-role-mapping, which has two columns (person & role) to list the pairs. The data in this table is obviously generated from the Person.java class, going through the set of roles that a person can have. For example, a person called Jake can have a set of roles, be they a student, a reseacher, an employee.. So in the person-role-mapping table, jake-student is in a row, jake-researcher is in a row, jake-employee is in row..
Of course there will be more in the table, since a role, e.g. a student, can be possessed by many persons.
NOW, here is the problem I am stuck with...
I want to use a HQL query to ask the person-role-mapping table to give a list of persons that have a same role, e.g. please return all the persons who are students to me..
Since this person-role-mapping table is not directly mapped from any class as described above, I was really struggling with how I can use HQL to make this query..
I do really appreciate if someone could help me out with this problem.
Many thanks, Jake
|