tags:

views:

15

answers:

1

i have two tables... i want to get data from second table using primary key of first table...that primary key was act as foreign key in second table...how can retrieve from that table in hibernate...

+1  A: 

You need to use Hibernate's association mappings.

For example, in an app I'm writing now I have a job table which has a user_id column. This is a foreign key to the id column in a user table. Therefore in my Job entity I have a User member variable, and have my annotation on the getUser() method as follows.

@ManyToOne
@JoinColumn(name = "user_id")
public User getUser() {
    return this.user;
}

I can therefore retrieve a Job, then simply call getUser() to get the user.

Before switching to annotations, I was using the following XML to achieve the above in the hmb file for the entity.

<many-to-one name="user" column="user_id" not-null="true"/>
William
what should we specify in *.hbm.xml file
Kandhasamy
I have added the XML I was using before switching to annotations.
William