Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoiding duplication for JoinTable annotation

 
James Johnsona
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have one table, the JOB_DEPENDENCY table that has columns parent_id and child_id which represent IDs of a job. You can query this table to find the children or parents of a given job. Eg, to find children for a job, select child_id FROM JOB_DEPENDENCY where parent_id = ??? and so on.
The job has its own table which has all the properties of the job including the job_id (which is the same ID that would go in the child/parent id of the dependency table). Pretty natural setup everything as you would expect...


Here is the issue, inside the Job entity, I have this annotation to get me the children of a given job



Loads and persists fine as you would expect updating the rows whenever this collection is changed with the new table.
The issue is, I also need a map of the ancestors of a given job. So I would add this:




What I'm worried now is about duplication. If A is the parent of B, when entity A is persisted with its children map, it should have a row in the JOB_DEPENDENCY table with parent_id = A's ID, and child_id = B's ID.
Likewise, when B is persisted as a child of A, with its parents map, it should have a row in the JOB_DEPENDENCY table with parent_id = A's ID, and child_id = B's ID. Will this result in duplicate rows now when both A and B are persisted?

This is what I am hypothesizing will happen, and was wondering if there was any elegant way around this? Maybe I don't need both maps. Maybe, there is a way in hibernate to not persist one of the maps. Etc. I still need both loaded to see the parents/children for a given job.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic