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

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

org.hibernate.cfg 
Class Configuration

An instance of Configuration配置 allows theapplication应用 tospecify指定 properties性能 andmapping映射 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 servicingclient客户 requests. The Configuration is meant only as an initialization-time object. SessionFactorys areimmutable不变的 and do notretain保持 any association back to the Configuration.

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

buildSessionFactory

Instantiate举例说明 a using new SessionFactory, the properties and mappings in this configuration. The SessionFactory will be immutable, so changes made to the Configurationafter building the SessionFactory will not affect 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 is set设置. This internal state includes all of the metadata元数据 about Object/Relational Mapping.

Implementors must be threadsafe.

org.hibernate 
Interface Session

The mainruntime运行时 interface between a Java application and Hibernate. This is the central API classabstracting摘要 thenotion概念 of apersistence持续 service.

The lifecycle生命周期 of a Session is bounded有界限 by the beginning and end of alogical合理的 transaction事务. (Long transactions might spanseveral几个 database .)
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.

转载地址:http://hhzli.baihongyu.com/

你可能感兴趣的文章
【iOS游戏开发】icon那点事 之 图标设计(三)
查看>>
【IOS游戏开发】之测试发布(Distribution)
查看>>
【IOS游戏开发】之IPA破解原理
查看>>
【一天一道LeetCode】#45. Jump Game II
查看>>
【一天一道LeetCode】#46. Permutations
查看>>
【一天一道LeetCode】#47. Permutations II
查看>>
【一天一道LeetCode】#48. Rotate Image
查看>>
【一天一道LeetCode】#56. Merge Intervals
查看>>
【一天一道LeetCode】#57. Insert Interval
查看>>
【一天一道LeetCode】#58. Length of Last Word
查看>>
【一天一道LeetCode】#59. Spiral Matrix II
查看>>
【一天一道LeetCode】#30. Substring with Concatenation of All Words
查看>>
【一天一道LeetCode】#60. Permutation Sequence.
查看>>
【一天一道LeetCode】#62. Unique Paths
查看>>
【一天一道LeetCode】#61. Rotate List
查看>>
【一天一道LeetCode】#63. Unique Paths II
查看>>
【一天一道LeetCode】#36. Valid Sudoku
查看>>
【一天一道LeetCode】#75. Sort Colors
查看>>
【一天一道LeetCode】#76. Minimum Window Substring
查看>>
【计算机网络 第五版】阅读笔记之一:概述
查看>>