Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parent - Child relationships

 
Anil Suvarthan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anil Suvarthan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second link is what you would need. It works the same except the FK relationship in the table is to itself.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic