发布于 2015-07-28 15:24:27 | 398 次阅读 | 评论: 0 | 来源: 网络整理

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.
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务