hibernate

--Configuration

An instance ofConfigurationallows the application to specify(指定)properties(属性) and mapping documents to be used when creating aSessionFactory. Usually an application will create a singleConfiguration, build a single instance ofSessionFactoryand then instantiateSessions in threads servicing client requests. TheConfigurationis meant only as an initialization-time(初始化) object.SessionFactorys are immutable(不变的)and do not retain any association back to theConfiguration.


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

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

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



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

Instantiate(实例)a newSessionFactory, using the properties(属性)andmappingsin this configuration(配制). TheSessionFactorywill be

immutable(不可变的), so changes made to theConfigurationafter building theSessionFactorywill notaffect(影响)it.


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

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


--org.hibernate.SessionFactory

The main contract here is the creation ofSessioninstances(实例). Usually an application(应用程序)has a singleSessionFactoryinstance and threads servicing client requests obtainSessioninstances from this factory.


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

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


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

Implementorsmustbe 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 aSessionis bounded(有限制的)by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)

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

The main function of theSessionis 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 anySession
persistent(持久):associated(相关的)with a unique(仅有的)Session
detached(托管):previously persistent(先前联系的), notassociated(有关联的)with anySession

Transient instances may be made persistentby callingsave(),persist()orsaveOrUpdate().Persistent(持续的) instances may be made transient by callingdelete(). Any instance returned by aget()orload()method is persistent. Detached instances may be made persistent(持久)by callingupdate(),saveOrUpdate(),lock()orreplicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by callingmerge().


瞬时可以持久化通过调用save(),persist()或者saveOrUpdate()。持久可以瞬时通过调用delete(),

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

瞬时或者托管的状态可以持久以一个新的持久化通过调用merge();




save()andpersist()result in an SQLINSERT,delete()in an SQLDELETEandupdate()ormerge()in an SQLUPDATE. Changes topersistent(持久的)instances aredetected(设置)at flush time and also result in an SQLUPDATE.saveOrUpdate()andreplicate()result in either anINSERToranUPDATE.


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

持久化的变化是被设置的在同步的时候和也许结果使用的是一个SQLUPDATE.saveOrUpdate()andreplicate(), 结果就在insert 和 Update之间的一个

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


它没打算使用者的线程安全,作为每一个线程/事物可以获得属于自己的实例从一个SessionFactory

ASessioninstance 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 theSessionthrows an exception, the transaction must be rolled back and the session discarded(抛弃的). Theinternal(内部的)state of theSessionmight 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 aSessionand is usuallyinstantiated(实例化)by a call toSession.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 uncommittedTransactionassociated with a particularSessionat any time.

Implementors are not intended to be threadsafe.


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

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

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









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值