public interface Session extends SharedSessionContract,EntityManager,HibernateEntiryManager,AutoCloseable;
主要用于java 應(yīng)用和hibernate之間運(yùn)行時(shí)的接口萝招;這是一個(gè)核心的持久化服務(wù)API抽象類(lèi)概念惶翻;
一個(gè)session的生命周期主要由邏輯事務(wù)管理器的開(kāi)始和結(jié)束界定;
session的主要作用是為映射的實(shí)體類(lèi)實(shí)例提供create ,read and delete 操作渺绒;實(shí)例可能以下面三種方式的一種狀態(tài)存在:
transient:從不持久化,不與任何session關(guān)聯(lián)
persistent:與一個(gè)唯一性的session關(guān)聯(lián)
detached:前置持久,不與任何session關(guān)聯(lián)
transient實(shí)例可能通過(guò)調(diào)用sav(),persist(),or saveOrUpdate()進(jìn)行持久化峡捡;Persistent實(shí)例可能通過(guò)transient調(diào)用delete();任何實(shí)例返回一個(gè)由get()或者load()方法返回的持久化對(duì)象劣纲;Detached實(shí)例可能通過(guò)update(),saveOrUpdate(),lock() or replicate()進(jìn)行實(shí)例化逢捺;在transient or detached狀態(tài)下的實(shí)例也可以通過(guò)調(diào)用merge()方法,作為一個(gè)新的持久化對(duì)象進(jìn)行持久化癞季;
save()和persist()在SQL中執(zhí)行的結(jié)果是INSERT,delete()在SQL中執(zhí)行的結(jié)果是DELETE;update()或merge()在SQL中執(zhí)行的是UPDATE劫瞳。持久化實(shí)例在寫(xiě)入時(shí)間段被發(fā)現(xiàn)發(fā)生變化倘潜,且在SQL中執(zhí)行了UPDATE。saveOrUpdate()和replicate()執(zhí)行INSERT或者UPDATE中一種行為志于;
實(shí)現(xiàn)的類(lèi)并不是線(xiàn)程安全的涮因;因此,每個(gè)線(xiàn)程或者事務(wù)應(yīng)該從sessionfactory獲取自己的實(shí)例伺绽;
一個(gè)Session實(shí)例是序列化的养泡,如果他的持久類(lèi)是序列化的;
一個(gè)典型的事務(wù)管理應(yīng)該使用如下的格式:
Session sess = factory.openSession();
Transaction tx;
try{
tx = sess.beginTransaction();
//do some work
tx.commit();
}catch(Exception e){
if(tx!=null)tx.rollback();
throw e;
}finally{
sess.close();
}
具體方法如下
All MethodsInstance MethodsAbstract MethodsDeprecated Methods
Modifier and TypeMethod and Description
voidaddEventListeners(SessionEventListener...?listeners)
Add one or more listeners to the Session
Session.LockRequestbuildLockRequest(LockOptionslockOptions)
Build a LockRequest that specifies the LockMode, pessimistic lock timeout and lock scope.
IdentifierLoadAccessbyId(Class?entityClass)
Create anIdentifierLoadAccessinstance to retrieve the specified entity by primary key.
IdentifierLoadAccessbyId(StringentityName)
Create anIdentifierLoadAccessinstance to retrieve the specified entity type by primary key.
MultiIdentifierLoadAccessbyMultipleIds(Class?entityClass)
Create aMultiIdentifierLoadAccessinstance to retrieve multiple entities at once as specified by primary key values.
MultiIdentifierLoadAccessbyMultipleIds(StringentityName)
Create aMultiIdentifierLoadAccessinstance to retrieve multiple entities at once as specified by primary key values.
NaturalIdLoadAccessbyNaturalId(Class?entityClass)
Create anNaturalIdLoadAccessinstance to retrieve the specified entity by its natural id.
NaturalIdLoadAccessbyNaturalId(StringentityName)
Create anNaturalIdLoadAccessinstance to retrieve the specified entity by its natural id.
SimpleNaturalIdLoadAccessbySimpleNaturalId(Class?entityClass)
Create anSimpleNaturalIdLoadAccessinstance to retrieve the specified entity by its simple (single attribute) natural id.
SimpleNaturalIdLoadAccessbySimpleNaturalId(StringentityName)
Create anSimpleNaturalIdLoadAccessinstance to retrieve the specified entity by its natural id.
voidcancelQuery()
Cancel the execution of the current query.
voidclear()
Completely clear the session.
booleancontains(StringentityName,Objectobject)
Check if this entity is associated with this Session.
QuerycreateFilter(Objectcollection,StringqueryString)
Create aQueryinstance for the given collection and filter string.
QuerycreateNamedQuery(Stringname,Class?resultType)
The JPA-defined named, typed query creation method.
QuerycreateQuery(CriteriaDeletedeleteQuery)
QuerycreateQuery(CriteriaQuery?criteriaQuery)
QuerycreateQuery(CriteriaUpdateupdateQuery)
QuerycreateQuery(StringqueryString)
Create aQueryinstance for the given HQL/JPQL query string.
QuerycreateQuery(StringqueryString,Class?resultType)
Create a typedQueryinstance for the given HQL/JPQL query string.
Remove a persistent instance from the datastore.
voiddelete(StringentityName,Objectobject)
Remove a persistent instance from the datastore.
voiddisableFetchProfile(Stringname)
Disable a particular fetch profile on this session.
voiddisableFilter(StringfilterName)
Disable the named filter for the current session.
Disconnect the session from its underlying JDBC connection.
?TdoReturningWork(ReturningWork?work)
Controller for allowing users to perform JDBC related work using the Connection managed by this Session.
Controller for allowing users to perform JDBC related work using the Connection managed by this Session.
voidenableFetchProfile(Stringname)
Enable a particular fetch profile on this session.
FilterenableFilter(StringfilterName)
Enable the named filter for this current session.
Remove this instance from the session cache.
voidflush()
Force this session to flush.
?Tget(Class?entityType,Serializableid)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
?Tget(Class?entityType,Serializableid,LockModelockMode)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
?Tget(Class?entityType,Serializableid,LockOptionslockOptions)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
Objectget(StringentityName,Serializableid)
Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.
Objectget(StringentityName,Serializableid,LockModelockMode)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
Objectget(StringentityName,Serializableid,LockOptionslockOptions)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
Get the current cache mode.
LockModegetCurrentLockMode(Objectobject)
Determine the current lock mode of the given object.
FiltergetEnabledFilter(StringfilterName)
Retrieve a currently enabled filter by name.
StringgetEntityName(Objectobject)
Return the entity name for a persistent entity.
For users of the Hibernate native APIs, we've had to rename this method as defined by Hibernate historically because the JPA contract defines a method of the same name, but returning the JPAFlushModeTyperather than Hibernate'sFlushMode.
FlushModegetHibernateFlushMode()
Get the current flush mode for this session.
SerializablegetIdentifier(Objectobject)
Return the identifier value of the given entity as associated with this session.
Retrieve this session's helper/delegate for creating LOB instances.
SessionFactorygetSessionFactory()
Get the session factory which created this session.
SessionStatisticsgetStatistics()
Get the statistics for this session.
Convenience access to theTypeHelperassociated with this session'sSessionFactory.
booleanisDefaultReadOnly()
Will entities and proxies that are loaded into this session be made read-only by default? To determine the read-only/modifiable setting for a particular entity or proxy:
booleanisDirty()
Does this session contain any changes which must be synchronized with the database? In other words, would any DML operations be executed if we flushed this session?
booleanisFetchProfileEnabled(Stringname)
Is a particular fetch profile enabled on this session?
booleanisReadOnly(ObjectentityOrProxy)
Is the specified entity or proxy read-only? To get the default read-only/modifiable setting used for entities and proxies that are loaded into the session:
?Tload(Class?theClass,Serializableid)
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.
?Tload(Class?theClass,Serializableid,LockModelockMode)
Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
?Tload(Class?theClass,Serializableid,LockOptionslockOptions)
Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
voidload(Objectobject,Serializableid)
Read the persistent state associated with the given identifier into the given transient instance.
Objectload(StringentityName,Serializableid)
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.
Objectload(StringentityName,Serializableid,LockModelockMode)
Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
Objectload(StringentityName,Serializableid,LockOptionslockOptions)
Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
voidlock(Objectobject,LockModelockMode)
Obtain the specified lock level upon the given object.
voidlock(StringentityName,Objectobject,LockModelockMode)
Obtain the specified lock level upon the given object.
Copy the state of the given object onto the persistent object with the same identifier.
Objectmerge(StringentityName,Objectobject)
Copy the state of the given object onto the persistent object with the same identifier.
Make a transient instance persistent.
voidpersist(StringentityName,Objectobject)
Make a transient instance persistent.
voidreconnect(Connectionconnection)
Reconnect to the given JDBC connection.
Re-read the state of the given instance from the underlying database.
voidrefresh(Objectobject,LockModelockMode)
Re-read the state of the given instance from the underlying database, with the givenLockMode.
voidrefresh(Objectobject,LockOptionslockOptions)
Re-read the state of the given instance from the underlying database, with the givenLockMode.
voidrefresh(StringentityName,Objectobject)
Re-read the state of the given instance from the underlying database.
voidrefresh(StringentityName,Objectobject,LockOptionslockOptions)
Re-read the state of the given instance from the underlying database, with the givenLockMode.
voidreplicate(Objectobject,ReplicationModereplicationMode)
Persist the state of the given detached instance, reusing the current identifier value.
voidreplicate(StringentityName,Objectobject,ReplicationModereplicationMode)
Persist the state of the given detached instance, reusing the current identifier value.
Serializablesave(Objectobject)
Persist the given transient instance, first assigning a generated identifier.
Serializablesave(StringentityName,Objectobject)
Persist the given transient instance, first assigning a generated identifier.
voidsaveOrUpdate(Objectobject)
Eithersave(Object)orupdate(Object)the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).
voidsaveOrUpdate(StringentityName,Objectobject)
Eithersave(String, Object)orupdate(String, Object)the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).
SharedSessionBuildersessionWithOptions()
Obtain aSessionbuilder with the ability to grab certain information from this session.
voidsetCacheMode(CacheModecacheMode)
Set the cache mode.
voidsetDefaultReadOnly(boolean?readOnly)
Change the default for entities and proxies loaded into this session from modifiable to read-only mode, or from modifiable to read-only mode.
voidsetFlushMode(FlushModeflushMode)
Deprecated.
(since 5.2) usesetHibernateFlushMode(FlushMode)instead
voidsetHibernateFlushMode(FlushModeflushMode)
Set the flush mode for this session.
voidsetReadOnly(ObjectentityOrProxy, boolean?readOnly)
Set an unmodified persistent object to read-only mode, or a read-only object to modifiable mode.
Update the persistent instance with the identifier of the given detached instance.
voidupdate(StringentityName,Objectobject)
Update the persistent instance with the identifier of the given detached instance.