*
The moose likes Object Relational Mapping and the fly likes Parent - Child relationships Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Parent - Child relationships" Watch "Parent - Child relationships" New topic
Author

Parent - Child relationships

Anil Suvarthan
Greenhorn

Joined: Aug 15, 2008
Posts: 5
Table:
------
id, name, subject, department.

Employee.java:
--------------
id(Column=id), name(column=name), subject(column=subject), List<Employee> (Mapping to department which returns list of employees from that department)

when i get the employee details based on the subject, it shud return the id, name, subject and list of other employees in that department.

We are using annotations. Can somebody help me with the mapping that needs to be given for the List<Employee> which is mapping to department.

Is it possible this way to return a list of the same objects?

Regards,
AK


Regards,<br />Anil
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Right now it looks like department is just a single field with one value. How can it hold a List.

You might have a Department table that in the Employee table has a FK id that maps to a record in the Department table. In that case you mappings should be ManyToOne from Employee to Department. but you cna also have a bi-directional association in Department that has a List of Employees.

But it looks like your table design doesn't support that and that your mappings doesn't show that.

For Employee to Department association look at this doc
http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html#d0e1177

For Department to List of Employee associations look at this doc
http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html#entity-mapping-association-collections

And it is possible to have a self-referencing table, and map that in itself.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Anil Suvarthan
Greenhorn

Joined: Aug 15, 2008
Posts: 5
I may have put it in the wrong way.

The values of the table looks like

id name subject Department
------------------------------------------------------
1 Amit Chemistry --
2 Mark Organic Chemi 1 (Chemistry subject row id)
3 Sunil Inorganic Chemi 1 (chemistry subject row id)

When i query the table with name="Amit" the return object shud look like

Employee
--------
id = 1
name = Amit
subject = Chemistry
List<Employee> employees = {new Employee(2, Mark, Organic Chemi)}, {new Employee(3, Sunil, Inorganic Chemi)} - Size 2

Can somebody help me with the mapping to be given .. I am using Annotations

Regards,
AK
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

The second link is what you would need. It works the same except the FK relationship in the table is to itself.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parent - Child relationships
 
Similar Threads
Joining tables
Simple requirement .. but I don't know how!
Simple requirement .. but I don't know how!
Parent-Child Confusion ....
simple example implementation?