It's actually very easy. All you need to do is set up associations between the different classes and mark the associations as SaveAutomatic = "true".
For example, in a sales order you typically have an order header and order lines. There would be a 1:N association from the order header to the lines and a 1:1 association from a line back to the header.
By making the 1:M order header->line association SaveAutomatic then by simply calling COrderHeader.Save() the framework will automatically save all of the lines related to the header.
Similarly you can automatically retrieve all the lines when you retrieve the header by marking the association and RetrieveAutomatic and you could delete the order and all it's lines automatically by using DeleteAutomatic.
I hope that helps, but if you want to provide a more specific example I am happy to have a look at it for you.
- Richard.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2004-05-20
Hi Richard,
Thanks for your speedy reply.
It helps.
I'm also refering to saving a CPersistentCollection.
I'll explain.
I'm using a CPersistentCollection to retrive all records in the database. Each record, item in the collection, is an object graph. Lots of childrens. I'm keeping the collection in my application in a variable of CPersistentCollection type, which after making changes to the data, I'd like to persist in the database. So, I'm looking for a method that is part of CPersistentCollection, for example, to ask it to save all items in the collection.
To overcome the situation, I read each itme in the CPersistentCollection in a foreach loop and save it.
It works fine, but I'm asking is there a better solution for it. Maybe the framework can do it better, efficiently?
Dan.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just to get things straight, you use a CRetrieveCriteria to get a number of objects from the database, process the resulting CCursor and put each retrieved object into a CPersistentCollection?
Now you want to call save on each item in the collection?
If I wrote something for this the framework would actually do the same thing (ie process the objects in a loop). The reason being that each object would be checked to see if it was dirty, a proxy objects, etc before being saved.
A CPersistentCollection is designed to look after the 1:M associations between classes. As such, the only real differences between a CPersistentCollection and a normal collection are:
1) It's strongly typed
2) It has a back reference to a parent object
3) When an item is added to/removed from the collection the parent object is marked as dirty.
I think the method you are using at the moment is fine, however if you want to simplify your code and just call a save method on the collection I am happy to add it.
-Richard.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2004-05-20
It's simpler then that. The following piece of code will explain:
....
CPersistentObject p = new MyObject();
CPersistentCollection pCol = p.GetAll();
...
And from there I use the data in my application.
Any modification is made on the objects contained by pCol variable. When I want to persist the data it would be nice to have a method call something like, pCol.save().
But, you're right. The same way I'm going through the collection to persist each object, would be done in the CPersistnetCollection class.
I just thought the code would look cleaner.
Thanks,
Dan
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi All,
I'v been tring to find a method to save all objects in a graph.
Is there any at all?
If not, it will be very handy to have one.
Thanks,
Dan
Hi Dan,
It's actually very easy. All you need to do is set up associations between the different classes and mark the associations as SaveAutomatic = "true".
For example, in a sales order you typically have an order header and order lines. There would be a 1:N association from the order header to the lines and a 1:1 association from a line back to the header.
By making the 1:M order header->line association SaveAutomatic then by simply calling COrderHeader.Save() the framework will automatically save all of the lines related to the header.
Similarly you can automatically retrieve all the lines when you retrieve the header by marking the association and RetrieveAutomatic and you could delete the order and all it's lines automatically by using DeleteAutomatic.
I hope that helps, but if you want to provide a more specific example I am happy to have a look at it for you.
- Richard.
Hi Richard,
Thanks for your speedy reply.
It helps.
I'm also refering to saving a CPersistentCollection.
I'll explain.
I'm using a CPersistentCollection to retrive all records in the database. Each record, item in the collection, is an object graph. Lots of childrens. I'm keeping the collection in my application in a variable of CPersistentCollection type, which after making changes to the data, I'd like to persist in the database. So, I'm looking for a method that is part of CPersistentCollection, for example, to ask it to save all items in the collection.
To overcome the situation, I read each itme in the CPersistentCollection in a foreach loop and save it.
It works fine, but I'm asking is there a better solution for it. Maybe the framework can do it better, efficiently?
Dan.
Sounds a little unusual...
Just to get things straight, you use a CRetrieveCriteria to get a number of objects from the database, process the resulting CCursor and put each retrieved object into a CPersistentCollection?
Now you want to call save on each item in the collection?
If I wrote something for this the framework would actually do the same thing (ie process the objects in a loop). The reason being that each object would be checked to see if it was dirty, a proxy objects, etc before being saved.
A CPersistentCollection is designed to look after the 1:M associations between classes. As such, the only real differences between a CPersistentCollection and a normal collection are:
1) It's strongly typed
2) It has a back reference to a parent object
3) When an item is added to/removed from the collection the parent object is marked as dirty.
I think the method you are using at the moment is fine, however if you want to simplify your code and just call a save method on the collection I am happy to add it.
-Richard.
It's simpler then that. The following piece of code will explain:
....
CPersistentObject p = new MyObject();
CPersistentCollection pCol = p.GetAll();
...
And from there I use the data in my application.
Any modification is made on the objects contained by pCol variable. When I want to persist the data it would be nice to have a method call something like, pCol.save().
But, you're right. The same way I'm going through the collection to persist each object, would be done in the CPersistnetCollection class.
I just thought the code would look cleaner.
Thanks,
Dan
Fair enough.
I'll add a method for it.
Hi Dan,
I've added a save and delete method to the CPersistentCollection.
Each object in the collection is processed and if an error occurs an exception is thrown.
Also, a transaction is used to ensure that the collection is saved as a single unit. ie either all objects are saved or none (if an error occurs).
Enjoy.