Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parent Child Relations with Join Table

 
Greg Pearman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a database table structure as such:

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

project
id
project_name

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
Posts: 167
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic