Hibernate几个常用方法官网释义 save、update、flush、load、merge、persist、delete、commit

1,  临时状态(Transient):用new创建的对象,它没有持久化,没有处于Session中,处于此状态的对象叫临时对象;

2,  持久化状态(Persistent):已经持久化,加入到了Session缓存中。如通过hibernate语句保存的对象。处于此状态的对象叫持久对象;

3,  游离状态(Detached):持久化对象脱离了Session的对象。如Session缓存被清空的对象。已经持久化,但不在Session缓存中。处于此状态的对象叫游离对象;

      × √

临时状态Transient

持久化状态Persistent

游离状态Detached

是否处于Session缓存中

        ×

         √

        ×

数据库中是否有对应记录

        ×

         √

     √

 

游离状态的实例可以通过调用save()、persist()或者saveOrUpdate()方法进行持久化。
持久化实例可以通过调用 delete()变成脱管状态。通过get()或load()方法得到的实例都是持久化状态的。
脱管状态的实例可以通过调用 update()、saveOrUpdate()、lock()或者replicate()进行持久化。

Transient instances may be made persistent by callingsave(),persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete(). Any instance returned by aget() orload() method is persistent. Detached instances may be made persistent by callingupdate(),saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by callingmerge().

save() and persist() result in an SQL INSERT, delete() in an SQLDELETE andupdate() or merge() in an SQL UPDATE. Changes topersistent instances are detected at flush time and also result in an SQLUPDATE.saveOrUpdate() and replicate() result in either anINSERT or anUPDATE.


Hibernate几个常用方法官网释义 save、update、flush、load、merge、persist、delete、commit_第1张图片

org.hibernate Interface Session:

--------------------------------save
Serializable save(Object object)
描述:
Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to(级联到) associated instances if the association is mapped with cascade="save-update"
参数:
object - a transient instance of a persistent class
返回:
the generated identifier
-------------
------------------------------update
void update(Object object)
描述:
Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown. This operation cascades to(级联到) associated instances if the association is mapped with cascade="save-update"
参数:
object - a detached instance containing updated state
-------------
------------------------------flush
void flush()throws HibernateException
描述:
Force this session to flush. Must be called at the end of a unit of work, before committing the transaction and closing the session (depending on setFlushMode(FlushMode), Transaction.commit() calls this method). Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.
抛出:
HibernateException - Indicates problems flushing the session or talking to the database.
--------------
------------------------------load
Object load(Class theClass,Serializable id)
描述:
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed.
You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.
参数:
theClass - a persistent class
id - a valid identifier of an existing persistent instance of the class
返回:
the persistent instance or proxy
---------------
------------------------------merge
Object merge(Object object)
描述:
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge"
The semantics(语义学) of this method are defined by JSR-220.
参数:
object - a detached instance with state to be copied
返回:
an updated persistent instance
---------------
------------------------------persist
void persist(Object object)
描述:
Make a transient instance persistent. This operation cascades to associated instances if the association is mapped with cascade="persist"
The semantics of this method are defined by JSR-220.
参数:
object - a transient instance to be made persistent
---------------
------------------------------delete
void delete(Object object)
描述:
Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state. This operation cascades to associated instances if the association is mapped with cascade="delete"
参数:
object - the instance to be removed

----------------

Serializable getIdentifier(Object object)
          Return the identifier value of the given entity as associated with this session.

 

org.hibernate Interface Transaction
-----------------------------commit
void commit()
Commit(提交) this transaction. This might entail(需要) a number of things depending on the context:
a.If this transaction(事务) is the initiator(引爆器), Session.flush() the Session with which it is associated (unless Session is in FlushMode.MANUAL).
b.If this transaction is the initiator, commit the underlying transaction.
c.Coordinate(调整) various callbacks(回调函数)
抛出:
HibernateException - Indicates a problem committing the transaction.

 

你可能感兴趣的:(Hibernate几个常用方法官网释义 save、update、flush、load、merge、persist、delete、commit)