当前位置:编程学习 > JAVA >>

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

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()进行持久化。
 
 
 
--------------------------------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)
描述:www.zzzyk.com
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
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,