Posted on 2008-11-01 10:09
Neil's NoteBook 阅读(77)
评论(0) 编辑 收藏
Transactional persistent instances (ie.
objects loaded, saved, created or queried by the Session
) may be manipulated by the application and any
changes to persistent state will be persisted when the Session
is flushed
(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. So the most straightforward way to update
the state of an object is to load()
it, and then
manipulate it directly, while the Session
is open:
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 since it would require both
an SQL SELECT
(to load an object) and an SQL UPDATE
(to persist its updated state) in the same session.
Therefore Hibernate offers an alternate approach, using detached instances.
Note that Hibernate does not offer its own API for
direct execution of UPDATE
or DELETE
statements. Hibernate is a state management service, you don't have to think
in statements to use it. JDBC is a perfect
API for executing SQL statements, you can get a JDBC Connection
at 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 may however
provide special mass operation functions. See Chapter 13, Batch processing for some
possible batch operation tricks.