From ce46e20efba053e914986f20e578d90ab2e42751 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 30 Nov 2010 18:47:05 +0200 Subject: Simplify hello example by using id returned from persist() --- doc/manual.xhtml | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/doc/manual.xhtml b/doc/manual.xhtml index fdc60c0..52f448d 100644 --- a/doc/manual.xhtml +++ b/doc/manual.xhtml @@ -1043,17 +1043,13 @@ main (int argc, char* argv[]) transaction t (db->begin ()); - db->persist (john); - db->persist (jane); - db->persist (joe); + // Make objects persistent and save their ids for later use. + // + john_id = db->persist (john); + jane_id = db->persist (jane); + joe_id = db->persist (joe); t.commit (); - - // Save object ids for later use. - // - john_id = john.id (); - jane_id = jane.id (); - joe_id = joe.id (); } } catch (const odb::exception& e) @@ -1109,7 +1105,12 @@ main (int argc, char* argv[]) Remember that we decided to use database-assigned identifiers for our person objects. The call to persist() is where this assignment happens. Once this function returns, the - id_ member contains this object's unique identifier.

+ id_ member contains this object's unique identifier. + As a convenience, the persist() function also returns + a copy of the object's identifier that it made persistent. We + save the returned identifier for each object in a local variable. + We will use these identifiers later in the chapter to perform other + database operations on our persistent objects.

After we have persisted our objects, it is time to commit the transaction and make the changes permanent. Only after the @@ -1130,15 +1131,6 @@ main (int argc, char* argv[]) automatically be rolled back and all the changes made to the database undone.

-

After the transaction has been committed, we save the - objects' identifiers in local variables. We will use them later in this - chapter to perform other database operations on our persistent - objects. You might have noticed that our person - class doesn't have the id() function that we use - here. To make our code compile we need to add a simple accessor - with this name that returns the value of the id_ - data member.

-

The final bit of code in our example is the catch block that handles the database exceptions. We do this by catching the base ODB exception (see Section 3.8, "ODB -- cgit v1.1