翻译hibernate---org.hibernate.session的API文档

public interface Session extends SharedSessionContract,EntityManager,HibernateEntiryManager,AutoCloseable;

主要用于java 应用和hibernate之间运行时的接口;这是一个核心的持久化服务API抽象类概念;

一个session的生命周期主要由逻辑事务管理器的开始和结束界定;

session的主要作用是为映射的实体类实例提供create ,read and delete 操作;实例可能以下面三种方式的一种状态存在:

transient:从不持久化,不与任何session关联

persistent:与一个唯一性的session关联

detached:前置持久,不与任何session关联

transient实例可能通过调用sav(),persist(),or saveOrUpdate()进行持久化;Persistent实例可能通过transient调用delete();任何实例返回一个由get()或者load()方法返回的持久化对象;Detached实例可能通过update(),saveOrUpdate(),lock() or replicate()进行实例化;在transient or detached状态下的实例也可以通过调用merge()方法,作为一个新的持久化对象进行持久化;

save()和persist()在SQL中执行的结果是INSERT,delete()在SQL中执行的结果是DELETE;update()或merge()在SQL中执行的是UPDATE。持久化实例在写入时间段被发现发生变化,且在SQL中执行了UPDATE。saveOrUpdate()和replicate()执行INSERT或者UPDATE中一种行为;

实现的类并不是线程安全的;因此,每个线程或者事务应该从sessionfactory获取自己的实例;

一个Session实例是序列化的,如果他的持久类是序列化的;

一个典型的事务管理应该使用如下的格式:

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.

voiddelete(Objectobject)

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.

Connectiondisconnect()

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.

voiddoWork(Workwork)

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.

voidevict(Objectobject)

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.

CacheModegetCacheMode()

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.

FlushModeTypegetFlushMode()

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.

LobHelpergetLobHelper()

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.

TypeHelpergetTypeHelper()

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.

Objectmerge(Objectobject)

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.

voidpersist(Objectobject)

Make a transient instance persistent.

voidpersist(StringentityName,Objectobject)

Make a transient instance persistent.

voidreconnect(Connectionconnection)

Reconnect to the given JDBC connection.

voidrefresh(Objectobject)

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.

voidupdate(Objectobject)

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.

你可能感兴趣的:(翻译hibernate---org.hibernate.session的API文档)