This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
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

Transitive Persistence

 
Sanjay Virkar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have two tables in a parent child relationship in MS SqlServer. Say Parent and Child.
Both have primary key of ID defined as a db type Identity.
In Child table there is field representing foreign key which is primary key for one of the
record in Parent table.


In Java we have two entity classes representing these two tables.
We use annotations for mapping relationships.

When we create new parent and new child and then set child in parent and execute save on parent, we are expecting both
to be saved in their own tables and newly created parents key automatically appearing in child table as a foreign key.

Representation of code in question looks as follows.

child l_child = new child (a,b,c,d);

parent l_parent = new parent (p,q,r);

l_parent.setChild(l_child);

l_parent.save();

At this point, both records are created correctly in database tables with their own keys (as a next sequence number)

But........

I was expecting parent's key automatically appearing in the child table too.


Before I cut an paste my actual source code I want to know if this is a valid expectation. (I thought that is what the transitive persistence achieves)

I could not find a good example of this on internet.

I did find an example where parent already existed and then it was found using Load and then adding a new child, did copy the parent's primary key to new child's foreign key.
But did not where both were created at the same time as a new records

Any clue what we could be doing wrong ?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before I cut an paste my actual source code I want to know if this is a valid expectation.


That is not a valid expectation.

I would recommend you to take a look at the this example to see how parent child relationship are managed (Insert Example). Also this example explains you the use of Inverse attribute for such mappings.

Hope this Helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic