博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Configuration、SessionFactory、Session
阅读量:6302 次
发布时间:2019-06-22

本文共 4276 字,大约阅读时间需要 14 分钟。

org.hibernate.cfg Class Configuration

An instance of Configuration allows(允许)  the application to specify properties and mapping documents to be used when creating a SessionFactory. Usually an application will create a single Configuration, build a single instance of SessionFactory and then instantiate Sessions in threads servicing client requests. The Configuration is meant(意味着) only as an initialization-time object. SessionFactorys are immutable anddo not retain(不保留) anyassociation(关联) back to the Configuration.

A new Configuration will use the properties specified in hibernate.properties by default.

buildSessionFactory

Instantiate a new SessionFactory, using the properties and mappings in this configuration. The SessionFactory will be immutable, so changes made to the Configuration after building the SessionFactory will notaffect(影响) it.

org.hibernate 
Interface SessionFactory

The main contract here is the creation of  instances. Usually an application has a single  instance and threads servicing client requests obtain(获得,得到) instances from this factory.

The internal state of a  is immutable. Once(一旦) it is created this internal state isset(设置的). This internal state includes all of themetadata(元数据)about Object/Relational Mapping.

Implementors must be threadsafe.

org.hibernate 
Interface Session

The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion(概念) of a persistence(持久化) service.

The lifecycle(生命周期) of a Session is bounded(有界限) by the beginning and end of a logical(逻辑) transaction. (Long transactions might span(跨越,持续,有) several(多个) database transactions.)

The main function(功能,用途,函数) of the Session is to offer(提供) create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:
transient(短暂的,瞬时的): never
persistent(持续,持久,固执的), not associated(关联) with any Session
persistent: 
associated with(与..相关) a unique Session
detached(分离的): 
previously(以前) persistent, not associated with any Session
Transient instances may be made persistent by calling save()persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete(). Any instance returned by a get() or load() method is persistent. Detached instances may be made persistent by calling update()saveOrUpdate()lock() or replicate(). The state of a 
transient or detached instance may also be made persistent as a new persistent instance by calling merge().
save() and persist() result in an SQL INSERTdelete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances aredetected(检测到) at flush time and also result in an SQL UPDATEsaveOrUpdate() and replicate() result in either(任何一个) an INSERT or an UPDATE.
It is not intended that implementors be threadsafe. Instead(相反) each thread/transaction shouldobtain(获得,获取) its own instance from a SessionFactory.
Session instance is serializable if its persistent classes are serializable.

A typical(典型的) transaction should use the following idiom:

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(); }
If the 
Session
 throws an exception, the transaction must be rolled back and the session
discarded(废弃,丢弃)
. The internal state of the Session might not be consistent(不一致) with the database after the exception
occurs(发生)
.

org.hibernate 
Interface Transaction

Allows the application to define
units of work(操作单元)
, while
maintaining(维护)
abstraction from the
underlying(底层的)
transaction implementation (eg. JTA, JDBC).
A transaction is
associated(关联)
with a Session and is usually instantiated by a call to Session.beginTransaction(). A single session might span multiple transactions since the notion of a session (a
conversation(对话)
between the application and the datastore) is of coarser(粗糙) granularity(粒度) than the notion of a transaction. However, it is intended that there be at most one uncommitted Transaction associated with a particular Session at any time.
Implementors are not intended to be threadsafe.

转载于:https://www.cnblogs.com/jpa2/archive/2012/04/17/2527448.html

你可能感兴趣的文章
【网络文摘】2016年里做前端是怎样一种体验
查看>>
[充电]C++ string字符串替换
查看>>
ol新属性
查看>>
[Angular2 Form] Reactive Form, show error message for one field
查看>>
MyBatis之级联——鉴别器
查看>>
关于项目数据库设计--投票系统
查看>>
MySql优化--使用索引优化
查看>>
[Go] 数组 和 切片(array、slice)
查看>>
关于Cocos2d-x运行项目时弹框崩溃的解决
查看>>
时间管理的基本程序
查看>>
phalcon 前端代码结构
查看>>
Oracle round函数是什么意思?怎么运用?
查看>>
Hadoop Hive概念学习系列之hive里的扩展接口(CLI、Beeline、JDBC)(十六)
查看>>
su su- sudo的区别
查看>>
Linux SD/MMC/SDIO驱动分析
查看>>
netty 解决TCP粘包与拆包问题(一)
查看>>
逻辑斯特回归(logistic regression)与最大熵模型(maximum entropy model)
查看>>
Javascript隐式转换
查看>>
mac brew install redis 报错
查看>>
nginx系统真正有效的图片防盗链完整设置详解
查看>>