Home  /  Questions  /  Question

50   50
Aug 05, 2014

Composite Pattern Persistence

What is the best way to persist a tree object to a SQLtable with as few database roundtrips as possible?

508   99.9
Aug 19, 2014
Without knowing the details of your data model, I would suspect there is no pattern-based shortcut.  

Most likely you'll have to iterate over the nodes and save the appropriate changes (INSERT, UPDATE, or DELETE). However, having said this, the tree iteration can be done with the Iterator pattern.

Another challenge is when moving nodes within the tree. This may require fairly extensive adjustments of  node records in the database (again depending on your data model)..

Joe Celko wrote a great SQL book discussing tree structures.  Google his name and you'll find it (or search Amazon).