本文共 4121 字,大约阅读时间需要 13 分钟。
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.
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.
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.
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 INSERT, delete() 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 UPDATE. saveOrUpdate() 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. A 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(); }
转载地址:http://hhzli.baihongyu.com/