This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes simple example implementation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "simple example implementation?" Watch "simple example implementation?" New topic
Author

simple example implementation?

yamini nadella
Ranch Hand

Joined: Apr 13, 2004
Posts: 257
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?.
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
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.


%
Franck McGeough
Greenhorn

Joined: Oct 27, 2005
Posts: 26
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.
 
 
subject: simple example implementation?
 
Similar Threads
one-to-one hibernate
Design question regarding managing one-to-many relationship...
question about Enity mapping
Oracle Forign Key Basic .
Issue while update the parent recored and removed some child records