File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Parent Child Relations with Join Table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Parent Child Relations with Join Table" Watch "Parent Child Relations with Join Table" New topic

Parent Child Relations with Join Table

Greg Pearman

Joined: Apr 09, 2012
Posts: 1
I have a database table structure as such:

parent_project_id (foreign key to project table)
child_project_id (foreign key to project table)


I have a JPA entity

I would like to add mapping such that I can use the existing database tables as is and still map a parent-child relationship where by a project can be a parent to multiple other projects and can also be a child to at most one other project. So ultimately I need somewhere to have a mapping of a collection of projects to represent the child projects associated to a given project and a parentProjectId to hold the relationship of a parent to a given project is one exists.

I don't know if I can just map this directly in the Project entity or if I need to create a new ProjectMapping entity instead and do the mapping there and what the mappings should look like depending on where they are implemented. Is it possible to do this just in the Project entity?
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 128

there are 3 ways, you should decide which one to use-
- First one is to have a new field in Project class to have the Parent Project.
- Second option is to have a collection of Child Projects in the project class.
- Both of the above.

You just need to put proper annotations so that JPA uses your existing tables & columns. No change at all will be required on database side. Hope that answers.
I agree. Here's the link:
subject: Parent Child Relations with Join Table
jQuery in Action, 3rd edition