Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

simple example implementation?

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yamini,

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.
 
Hey cool! They got a blimp! But I have a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic