aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Parent Child Relations with Join Table" Watch "Parent Child Relations with Join Table" New topic
Author

Parent Child Relations with Join Table

Greg Pearman
Greenhorn

Joined: Apr 09, 2012
Posts: 1
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

Joined: Mar 30, 2012
Posts: 124

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Parent Child Relations with Join Table
 
Similar Threads
Passed SCBCD 5.0 :)
Hibernate Bidirectional Mapping Problem.
Hibernate 3.3.1 - Delete doesn't work
retrieving object hierarchy - many-to-one?
Does WLS 6.1 support EJB2.0