Saving an object (inserts and updates)

To write an object into the database, call its save() method. Depending on whether the data object was newly created or was read from the database, the corresponding row in the database table is inserted or updated. If the object is new, its primary key(s) is/are generated automatically if the id method was set to "native" or "idbroker" in the schema.xml.

As an example, consider the following code, which creates an Author object and inserts it into the database.

Author stevens = new Author();
stevens.setFirstName("W.");
stevens.setLastName("Stevens");
stevens.save();

If the generator properties "torque.complexObjectModel" and "torque.objectIsCaching" were not set to false at generation time, any objects which are referenced in the foreign key Lists of the object are also saved.

For example, in the following code, calling book.save() also saves the author added to the book:

Author bloch = new Author();
bloch.setFirstName("Joshua");
bloch.setLastName("Bloch");
Book effective = new Book();
effective.setTitle("Effective Java");
effective.setISBN("0-618-12902-2");
effective.setPublisher(addison);
bloch.addBook(effective);
bloch.save(); //also saves the book "effective"

Note that the save is only propagated in the 1->n directions of foreign keys, not in the n->1 direction. I.e. in the above example, calling effective.save() would NOT save the corresponding author.

If the object which save method is called is neither new nor modified, it is not saved. Internally, this is handled via the isNew and modified flags of the object.

There are alternative ways to insert or update an object in the database: you can pass the objects to the doInsert() or doUpdate() methods of their corresponding Peers, or you can create a ColumnValues object which contains the data of the object and pass the Criteria object to the doInsert() or doUpdate() methods of the Peer class. Note that if you construct a ColumnValues object for a new object, the id field need not be added to the ColumnValues; it is taken care of by Torque internally.

Mass updates

If you want to update many objects at once, use the doUpdate(Criteria, ColumnValues) method in BasePeer. For example, to set the name attribute to null for all authors with an id greater than 5, use:

Criteria criteria = new Criteria();
criteria.where(AuthorPeer.AUTHOR_ID, 5, Criteria.GREATER_THAN);
ColumnValues columnValues = new ColumnValues();
columnValues.put(AuthorPeer.NAME,
    new JdbcTypedValue(
        null,
        java.sql.Types.VARCHAR));
BasePeer.doUpdate(criteria, columnValues);

Deleting objects

If you want to delete an author which was already loaded into memory, then simply pass the object to the doDelete method of the peer:

AuthorPeer.doDelete(author);

You do not need to load authors to delete them, you can also use a Criteria object to perform deletes. For example, to delete the author with the id 3 without loading the author object, use:

Criteria criteria = new Criteria();
criteria.where(AuthorPeer.AUTHOR_ID, 3);
AuthorPeer.doDelete(criteria);

The latter method can also be used for mass deletes. E.g. to delete all authors with an id greater than 5, use:

Criteria criteria = new Criteria();
criteria.where(AuthorPeer.AUTHOR_ID, 5, Criteria.GREATER_THAN);
AuthorPeer.doDelete(criteria);