This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Originally posted by yamini nadella: assume there are two tables employee(empid, empname, deptid) and department(deptid, deptname). There is primarykey and foriegn key relationship is there between these two tables.
How can I do mapping in such a way if I search empid then I get both employee and department values?
do I need to have bean classes employee and department seperately or one class only? Do I need to have 2 mapping files for each table or one mapping file?
If I need to define relationship then I need to define many to one or what relationship?
unfortunately in many hibernate books things are not clear?.
I think the HTML docs that come with Hibernate are excellent. They show how to set up 1:many relationships like this.
You can have separate Employee and Department classes. Yes, they'll each have their own Hibernate XML mapping files.
Each table is associated with a single Java class. The relationship between Department and Employee (in what you describe) is many-to-one. That is, a Department may have many Employees. In the Java classes Department would be contain a Set of Employees. Employee would contain a Department.
Department's relationship to Employee might be modeled like this in mapping file :
The column name is the foreign key stored in Employee that relates the Employee to a Department.
In the Employee mapping file you might have something that looks like this :
Notice that the column name in this case is the same foreign key contained in Employee. Hibernate "knows" the primary key of Department so it executes the correct sql (in your case, the column names are the same so its not clear what deptid the mapping doc refers to - it's referring to the Employee table).
Once you fetch the Employee using empid you can call getDepartment(). This will return the Department object.
The doc's take a while to get through. Keep after for a couple weeks and it will get clearer.