📜  Hibernate-会话

📅  最后修改于: 2020-11-16 06:57:39             🧑  作者: Mango


会话用于获得与数据库的物理连接。 Session对象是轻量级的,旨在在每次需要与数据库进行交互时实例化。持久对象通过Session对象保存和检索。

会话对象不应长时间保持打开状态,因为它们通常不是线程安全的,因此应根据需要创建和销毁它们。会话的主要函数是为映射实体类的实例提供,创建,读取和删除操作。

在给定的时间点,实例可能存在以下三种状态之一-

  • 瞬态-持久类的新实例,该实例与会话无关,并且在数据库中没有表示,并且Hibernate认为没有标识符值是瞬态的。

  • 持久-您可以通过将临时实例与会话相关联来使该实例持久化。持久实例在数据库中具有表示形式,标识符值,并且与会话相关联。

  • 分离-关闭Hibernate会话后,持久实例将变为分离实例。

如果会话实例的持久类是可序列化的,则它是可序列化的。典型的事务应使用以下习惯用法-

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界面提供了许多方法,但是我仅列出了一些重要的方法,我们将在本教程中使用这些方法。您可以查看Hibernate文档中与SessionSessionFactory相关的方法的完整列表。

Sr.No. Session Methods & Description
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.