hibernate

来源:互联网 发布:淘宝店被差评怎么办 编辑:程序博客网 时间:2024/06/05 15:13

--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 aninitialization-time(初始化) object. SessionFactorys are immutable(不变的) and do not retain any association back to the Configuration.


一个配置实例, 被用于允许应用程序指定属性和映射文件当创建了一个SessionFactory;

通常一个应用将创建单个Configuration配置,创建单个SessionFactory实例 和然后实例化session在线程中提供给客户,

Configuration 的意义作为一个初始化配置对象,SessionFactorys是不可改变的,和不能保留任何Configuration配置



--  org.hibernate.cfg.Configuration
buildSessionFactory方法

Instantiate(实例) a new SessionFactory, using the properties(属性) and mappings in thisconfiguration(配制). The SessionFactory will be 

immutable(不可变的), so changes made to the Configuration after building the SessionFactory will not affect(影响) it.


它是实例一个新的SessionFactory,用configuration映射属性。SessionFactory 是不改变的,

所以后面通过Configuration 新建的SessionFactory 将不受影响


--org.hibernate.SessionFactory

The main contract here is the creation of Session instances(实例). Usually an application(应用程序) has a single SessionFactory instance and threads servicing client requests obtainSession instances from this factory.


它主要的在这时创建一个session实例,通过一个应用程序有一个SessionFactory  实例

 线程提供给客户端请求obtainSession 实例从这个factory;


Theinternal(内部的) state of a SessionFactory isimmutable(不可改变的). Once it is created this internal state is set. This internal state includes all of themetadata(数据) about Object/Relational(相关的) Mapping.

Implementors must be threadsafe.


内部的SessionFactory 是不可改变的,一次它被创建在内部是设置好了,里面包括了所有的数据关于对象和相关的映射

 

--session

The main runtime interface between a Java application and Hibernate. This is the central API class abstracting thenotion(概念) of a 

persistence(持久的) service.

主要运行在java 和hibernate之间,这重要的API接口类抽象持久服务的概念


The lifecycle of a Session is bounded(有限制的) by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)

Session 的生命周期是有限制的通过事物的beginning和end

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:

seesion 函数主要功能提供create,read和delete操作为映射实体类的实例,实例也许存在三个中的一个



transient(瞬时的): neverpersistent(持续的), not associated with any Session
persistent(持久): associated(相关的) with a unique(仅有的) Session
detached(托管): previously persistent(先前联系的), not associated(有关联的) with any Session

Transient instances may be made persistentby 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()persist()或者saveOrUpdate()。持久可以瞬时通过调用delete(), 

任何实例返回一个get()或者load()方法是持久的,托管可以持久通过 update()saveOrUpdate()lock() or replicate()

瞬时或者托管的状态可以持久以一个新的持久化通过调用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 oran UPDATE.


save()和persist()的结果是有一个SQL insert,delete() ,SQL delete,Update() 或者是在SQL Update中的merge(),

持久化的变化是被设置的在同步的时候 和也许结果使用的是一个SQL UPDATEsaveOrUpdate() and replicate(), 结果就在insert 和 Update之间的一个

It is not intended thatimplementors(实现者) be threadsafe.Instead(作为..的替换) each thread/transaction shouldobtain(获得) its own instance from a SessionFactory.


它没打算使用者的线程安全,作为每一个线程/事物可以获得属于自己的实例从一个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(抛弃的). Theinternal(内部的) state of the Session might not beconsistent(一致的) with the database after the exception occurs.


假如这session 抛出了一个exception,这事物必须是要被回滚的和这session 是要被抛弃的,

内部的session 不可以一致的同这数据库在这exception 出现后


 

public interface Transaction

Allows theapplication(应用程序) todefine(定义) units of work, while maintaining abstraction from theunderlying(基础的) transaction 

implementation(执行) (eg. JTA, JDBC).

A transaction is associated(关联的) with a Session and is usuallyinstantiated(实例化)by a call to Session.beginTransaction(). A single session might span multiple transactions since thenotion(概念of a session (aconversation(会话) 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.


一个transaction是同一个session关联的,和同常是通过session.beginTransaction()实例化的。

一个session 也许包含多个事务,因为session的概念是...比这事务的概念。然而, 

它的目的是 最多这个事务没有提交用某个Session在任何时间






 


原创粉丝点击