Hibernate Sessions
Session對象用於獲取與數據庫的物理連接。 Session對象是重量輕,設計了一個互動是需要與數據庫每次被實例化。持久化對象被保存,並通過一個Session對象中檢索。
會話中的對象不應該保持開放很長一段時間,因為他們通常不被線程安全的,他們應該被創建並根據需要摧毀他們。這次會議的主要功能是提供創建,讀取和刪除操作映射的實體類的實例。實例中可能存在以下三種狀態之一在給定時間點:
-
短暫性: 持久化類的未與會話相關聯,並在數據庫中冇有代表性,冇有標識值的新實例被Hibernate認為是暫時的。
-
持久性: 可以做一個瞬態的實例持久化通過將它與一個會話相關聯。持久性實例都有一個表示在數據庫中,一個標識符值,與會話相關聯。
-
獨立性: 一旦我們關閉Hibernate的Session,持久化實例將成為一個分離的實例。
一個Session實例是可序列化的,如果它的持久化類是可序列化的。一個典型的事務應該使用下麵的語句:
Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); }
如果Session拋出異常,事務必須回滾,會話必須被丟棄。
Session 接口方法:
Session接口提供多個方法,但這裡列出的隻有少數重要的方法,這些方法我們在本教學中會使用。您可以查看Hibernate文檔Session和SessionFactory相關方法的完整列表。
S.N. | 會話的方法和說明 |
---|---|
1 |
Transaction beginTransaction() Begin a unit of work and return the associated Transaction object. |
2 |
void cancelQuery() Cancel the execution of the current query. |
3 |
void clear() Completely clear the session. |
4 |
Connection close() End the session by releasing the JDBC connection and cleaning up. |
5 |
Criteria createCriteria(Class persistentClass) Create a new Criteria instance, for the given entity class, or a superclass of an entity class. |
6 |
Criteria createCriteria(String entityName) Create a new Criteria instance, for the given entity name. |
7 |
Serializable getIdentifier(Object object) Return the identifier value of the given entity as associated with this session. |
8 |
Query createFilter(Object collection, String queryString) Create a new instance of Query for the given collection and filter string. |
9 |
Query createQuery(String queryString) Create a new instance of Query for the given HQL query string. |
10 |
SQLQuery createSQLQuery(String queryString) Create a new instance of SQLQuery for the given SQL query string. |
11 |
void delete(Object object) Remove a persistent instance from the datastore. |
12 |
void delete(String entityName, Object object) Remove a persistent instance from the datastore. |
13 |
Session get(String entityName, Serializable id) Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance. |
14 |
SessionFactory getSessionFactory() Get the session factory which created this session. |
15 |
void refresh(Object object) Re-read the state of the given instance from the underlying database. |
16 |
Transaction getTransaction() Get the Transaction instance associated with this session. |
17 |
boolean isConnected() Check if the session is currently connected. |
18 |
boolean isDirty() Does this session contain any changes which must be synchronized with the database? |
19 |
boolean isOpen() Check if the session is still open. |
20 |
Serializable save(Object object) Persist the given transient instance, first assigning a generated identifier. |
21 |
void saveOrUpdate(Object object) Either save(Object) or update(Object) the given instance. |
22 |
void update(Object object) Update the persistent instance with the identifier of the given detached instance. |
23 |
void update(String entityName, Object object) Update the persistent instance with the identifier of the given detached instance. |