These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: bidirectional assiociation
PostPosted: Thu Oct 23, 2008 7:50 pm 
Newbie

Joined: Wed Oct 22, 2008 10:20 pm
Posts: 11
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.