爲什麼要關注對象及其狀態?而不是sql語句?
Hibernate is a full object/relational mapping solution that not only shields the developer from
the details of the underlying database management system, but also offers state management
of objects. This is, contrary to the management of SQL statements in common JDBC/SQL
persistence layers, a natural object-oriented view of persistence in Java applications.
In other words, Hibernate application developers should always think about the state of their
objects, and not necessarily about the execution of SQL statements. This part is taken care of
by Hibernate and is only relevant for the application developer when tuning the performance of
the system.
Hibernate object states
Hibernate defines and supports the following object states:
• Transient - an object is transient if it has just been instantiated using the new operator, and it
is not associated with a Hibernate Session. It has no persistent representation in the database
and no identifier value has been assigned. Transient instances will be destroyed by the garbage
collector if the application does not hold a reference anymore. Use the Hibernate Session to
make an object persistent (and let Hibernate take care of the SQL statements that need to be
executed for this transition).
1 數據庫中不存在與瞬時對象相對應的記錄 (根據配置文件unsaved-value來判斷)
2 沒有hibernate根據配置文件設置主鍵生成生成策略分配的identifier值
• Persistent - a persistent instance has a representation in the database and an identifier value.
It might just have been saved or loaded, however, it is by definition in the scope of a Session.
Hibernate will detect any changes made to an object in persistent state and synchronize the
state with the database when the unit of work completes. Developers do not execute manual
UPDATE statements, or DELETE statements when an object should be made transient.
1 數據庫中存在與持久對象相對應的記錄
2 有hibernate根據配置文件設置主鍵生成生成策略分配的identifier值
3 通過執行saved (從對象到session)or loaded(從db到session)
4 因爲持久對象在session的管理範圍之內 ,hibernate 可以將 持久對象的任何改變在事務提交時同步到數據庫中(對持久對象調用update方法沒有意義)
• Detached - a detached instance is an object that has been persistent, but its Session has been closed. (注意這句話,如果這個對象在session被刪除了,那麼這個對象是什麼狀態?)The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at alater point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.
1 脫管對象 是曾經容納過這個持久對象的session,現在已經關閉了。(脫管對象有identifier值,)