翻译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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,029评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,238评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,576评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,214评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,324评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,392评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,416评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,196评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,631评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,919评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,090评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,767评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,410评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,090评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,328评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,952评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,979评论 2 351

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,459评论 5 6
  • 老大,张国 张国最大也不过大我三岁,不过人长的非常的成熟,那时候我们与一政府机关共享一个食堂,食堂的工作人员都很势...
    万能的老J阅读 444评论 1 5
  • 好好说话,是门学问。 成熟而准确的语言体系,是人类社会与动物最大的区别之一。语言帮助人们可以简单地表达自己的想法,...
    云夏KL阅读 150评论 0 0
  • 梅雪争春未肯降,骚人搁笔费评章。梅须逊雪三分白,雪却输梅一段香。 今天画的是雪梅 实在是喜爱 终于可以通过自己...
    虚空中的兔子阅读 834评论 15 23
  • 你说努力生活的意义到底是什么?当我十几岁的时候我想要的生活是那种特别文艺女轻年的生活,可是在我迈进了二十岁的门槛之...
    月光夕颜阅读 205评论 4 2