how can we achieve relationship in hibernate
i have two tables... i want to get data from second table using primary key of fir开发者_高级运维st table...that primary key was act as foreign key in second table...how can retrieve from that table in hibernate...
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"/>
精彩评论