Show Table of Contents
10.5. Modifying persistent objects
Transactional persistent instances (i.e. objects loaded, saved, created or queried by the
Session) can be manipulated by the application, and any changes to persistent state will be persisted when the
Sessionis flushed. This is discussed later in this chapter. There is no need to call a particular method (like
update(), which has a different purpose) to make your modifications persistent. The most straightforward way to update the state of an object is to
load()it and then manipulate it directly while the
DomesticCat cat = (DomesticCat) sess.load( Cat.class, new Long(69) ); cat.setName("PK"); sess.flush(); // changes to cat are automatically detected and persisted
Sometimes this programming model is inefficient, as it requires in the same session both an SQL
SELECTto load an object and an SQL
UPDATEto persist its updated state. Hibernate offers an alternate approach by using detached instances.
Hibernate does not offer its own API for direct execution of
DELETEstatements. Hibernate is a state management service, you do not have to think in statements to use it. JDBC is a perfect API for executing SQL statements, you can get a JDBC
Connectionat any time by calling
session.connection(). Furthermore, the notion of mass operations conflicts with object/relational mapping for online transaction processing-oriented applications. Future versions of Hibernate can, however, provide special mass operation functions. See Chapter 13, Batch processing for some possible batch operation tricks.