Hi,
I have 2 tables namely 'JOB' & PERSON'. There is a one to many relationship
between the 2 tables that is, different person can hold the same JOB.
E.g. Tom and Jenny are both accountant.
If given the job title (field in JOB table), I am able to find the list
of people holding the same job. How if I am given only the personID (field in PERSON table)
or name, how can I find the job title of that specific person?
I tried the bidirectional association method but I encountered
the following error:
"An association from the table PERSON refers to an unmapped class JOB."
Here are partial of my mapping:
Person.hbm.xml
--------------
...
<hibernate-mapping>
<class name="Person">
<id name="personId" type="int">
<generator class="increment"/>
</id>
<property name="personName" type="string"/>
<property name="address" type="string"/>
...
<many-to-one name="job" class="Job" column="jobId"/>
</class>
...
Job.hbm.xml
-----------
...
<hibernate-mapping>
<class name="Job">
<id name="jobId" type="int">
<generator class="increment"/>
</id>
<property name="jobTitle" type="string"/>
<set name="person" inverse="true" cascade="all">
<key column="jobId"/>
<one-to-many class="Person"/>
</set>
...
In my main test program, I have this:
public static void main(String[] args) {
...
String hql = "from Person p inner join p.Job as j where p.personId = '223'";
Query q = session.createQuery(hql);
return q.list();
//I tried to print the job title of that person here...but I encountered the error.
...
}
Is there anything wrong with my program?
|