文章目錄
-
public class Configuration
-
extends Object
-
implements Serializable
An instance of Configuration allows(允許) the application tospecify(指定) 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 meantonly(只有) as aninitialization(初始化)-timeobject. SessionFactorys
are immutable(不可變的) and do notretain(保留) anyassociation(關聯) back to the Configuration.
A new Configuration will use the properties
specified(詳細說明) in hibernate.properties(屬性)
by default.
buildSessionFactory
public SessionFactory buildSessionFactory() throws HibernateException
-
Instantiate(執行個體化) a new SessionFactory, using theproperties(屬性) andmappings(映射) in thisconfiguration(配置).
The SessionFactory will beimmutable(不可變的), so changes made to the Configurationafterbuilding(增加) the SessionFactory will
notaffect(影響) it.
-
-
-
Returns:
-
a new factory for Sessions
-
Throws:
-
HibernateException
-
See Also:
-
SessionFactory
org.hibernate
Interface SessionFactory
The main contract(合約) here is the
creation(創造) of Session instances. Usually an application has a single(一個)SessionFactory instance
and threads servicing client requestsobtain(獲得)Sessioninstances
from this factory.
The internal(內部的) 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.
-
Author:
-
Gavin King, Steve Ebersole
org.hibernate
Interface Session
-
All Superinterfaces:
-
Serializable
-
All Known Subinterfaces:
-
EventSource, Session
-
All Known Implementing Classes:
-
SessionImpl
-
public interface Session
-
extends Serializable
The main(主要的) runtime interface between a Java application and Hibernate. This is thecentral(集中的) API classabstracting(抽象) thenotion(概念)
of a persistence(持久性) service.
The lifecycle(生命週期) of a Session isbounded(有界限的) by the beginning and end of alogical(邏輯) transaction. (Long
transactionsmight(可能)
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 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
ineither(任何一個) an INSERT or an UPDATE.
It is not intended(打算) that implementors be threadsafe.Instead(反而) each thread/transaction should(應該)obtain(獲得)
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(); }
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(出現).