引用
save方法 public Serializable save(Object object) ????????????????? throws HibernateException
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". 持久化指定的游離態對象,首先要分配一個自動生成的主鍵標示符。 (或者如果主鍵生成器正在使用,使用當前主鍵標示符屬性值) 如果關聯關系映射配置了cascade="save-update"屬性,save操作對關聯對象級聯操作。 Parameters: object - a transient instance of a persistent class 一個要持久化類的自由狀態的實例對象 Returns: the generated identifier 返回生成的主鍵標示符 Throws: HibernateException
public static void insertUser(){Session sess = HibernateSessionFactory.getSession();User user = new User();Transaction tx = null;try{tx = sess.beginTransaction();// do some workuser.setLogonName("關羽");user.setNickName("關云長");user.setPassword("1234");sess.save(user);tx.commit();}catch (Exception e){if (tx != null)tx.rollback();}finally{sess.close();}}
/*** Concrete implementation of a Session, and also the central, organizing component* of Hibernate's internal implementation. As such, this class exposes two interfaces;* Session itself, to the application, and SessionImplementor, to other components* of Hibernate. This class is not threadsafe.** @author Gavin King*/
public final class SessionImpl extends AbstractSessionImpl implements EventSource, org.hibernate.classic.Session, JDBCContext.Context {//省略部分代碼// save() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~public void save(Object obj, Serializable id) throws HibernateException {save(null, obj, id);}public Serializable save(Object obj) throws HibernateException {return save(null, obj);}public Serializable save(String entityName, Object object) throws HibernateException {return fireSave( new SaveOrUpdateEvent(entityName, object, this) );}public void save(String entityName, Object object, Serializable id) throws HibernateException {fireSave( new SaveOrUpdateEvent(entityName, object, id, this) );}private Serializable fireSave(SaveOrUpdateEvent event) {errorIfClosed();checkTransactionSynchStatus();SaveOrUpdateEventListener[] saveEventListener = listeners.getSaveEventListeners();for ( int i = 0; i < saveEventListener.length; i++ ) {saveEventListener[i].onSaveOrUpdate(event);}return event.getResultId();}//省略部分代碼
}
引用
update方法 public void update(Object object) ??????????? throws HibernateException
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". 使用給定的游離狀態對象的主鍵標示符來更新持久化狀態實例對象。 如果存在相同主鍵標示符的持久化狀態對象,拋出異常。 如果關聯關系映射配置了cascade="save-update"屬性,update操作對關聯對象級聯操作。
Parameters: object - a detached instance containing updated state 包含了要更新狀態的游離狀態對象 Throws: HibernateException
/*** Concrete implementation of a Session, and also the central, organizing component* of Hibernate's internal implementation. As such, this class exposes two interfaces;* Session itself, to the application, and SessionImplementor, to other components* of Hibernate. This class is not threadsafe.** @author Gavin King*/
public final class SessionImpl extends AbstractSessionImpl implements EventSource, org.hibernate.classic.Session, JDBCContext.Context {//省略部分代碼// update() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~public void update(Object obj) throws HibernateException {update(null, obj);}public void update(Object obj, Serializable id) throws HibernateException {update(null, obj, id);}public void update(String entityName, Object object) throws HibernateException {fireUpdate( new SaveOrUpdateEvent(entityName, object, this) );}public void update(String entityName, Object object, Serializable id) throws HibernateException {fireUpdate(new SaveOrUpdateEvent(entityName, object, id, this));}private void fireUpdate(SaveOrUpdateEvent event) {errorIfClosed();checkTransactionSynchStatus();SaveOrUpdateEventListener[] updateEventListener = listeners.getUpdateEventListeners();for ( int i = 0; i < updateEventListener.length; i++ ) {updateEventListener[i].onSaveOrUpdate(event);}}//省略部分代碼
}
引用
控制臺查看Hibernate生成的sql語句如下: Hibernate: select user0_.id as id0_0_, user0_.logon_name as logon2_0_0_, user0_.nick_name as nick3_0_0_, user0_.password as password0_0_ from logon_user user0_ where user0_.id=? Hibernate: update logon_user set logon_name=?, nick_name=?, password=? where id=?
Read the persistent state associated with the given identifier into the given transient instance. 讀取和給定的主鍵標示符關聯的持久化狀態到給定的自由狀態的實例對象。 Parameters: object - an "empty" instance of the persistent class id - a valid identifier of an existing persistent instance of the class object - 一個持久化類的空對象 id - 一個已經存在的持久化實例對象的有效的主鍵標示符 Throws: HibernateException
public void saveOrUpdate(Object object) ????????????????? throws HibernateException Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking). This operation cascades to associated instances if the association is mapped with cascade="save-update". 可能對給定的實例對象執行save或者update方法,如果關聯關系映射配置了cascade="save-update"屬性,update操作對關聯對象級聯操作。
Parameters: object - a transient or detached instance containing new or updated state object - 包含新的或者已更新狀態的自由態或者是游離態的 對象實例 Throws: HibernateException See Also: save(Object), update(Object)
/*** Concrete implementation of a Session, and also the central, organizing component* of Hibernate's internal implementation. As such, this class exposes two interfaces;* Session itself, to the application, and SessionImplementor, to other components* of Hibernate. This class is not threadsafe.** @author Gavin King*/
public final class SessionImpl extends AbstractSessionImpl implements EventSource, org.hibernate.classic.Session, JDBCContext.Context {//省略部分代碼// saveOrUpdate() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~public void saveOrUpdate(Object object) throws HibernateException {saveOrUpdate(null, object);}public void saveOrUpdate(String entityName, Object obj) throws HibernateException {fireSaveOrUpdate( new SaveOrUpdateEvent(entityName, obj, this) );}private void fireSaveOrUpdate(SaveOrUpdateEvent event) {errorIfClosed();checkTransactionSynchStatus();SaveOrUpdateEventListener[] saveOrUpdateEventListener = listeners.getSaveOrUpdateEventListeners();for ( int i = 0; i < saveOrUpdateEventListener.length; i++ ) {saveOrUpdateEventListener[i].onSaveOrUpdate(event);}}//省略部分代碼
}
查看onSaveOrUpdate(event)方法的定義: Java代碼 ?
public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener implements SaveOrUpdateEventListener {private static final Log log = LogFactory.getLog( DefaultSaveOrUpdateEventListener.class );/*** Handle the given update event.** @param event The update event to be handled.*/public void onSaveOrUpdate(SaveOrUpdateEvent event) {final SessionImplementor source = event.getSession();final Object object = event.getObject();final Serializable requestedId = event.getRequestedId();if ( requestedId != null ) {//assign the requested id to the proxy, *before* //reassociating the proxyif ( object instanceof HibernateProxy ) {( ( HibernateProxy ) object ).getHibernateLazyInitializer().setIdentifier( requestedId );}}if ( reassociateIfUninitializedProxy( object, source ) ) {log.trace( "reassociated uninitialized proxy" );// an uninitialized proxy, noop, don't even need to // return an id, since it is never a save()}else {//initialize properties of the event:final Object entity = source.getPersistenceContext().unproxyAndReassociate( object );event.setEntity( entity );event.setEntry( source.getPersistenceContext().getEntry( entity ) );//return the id in the event objectevent.setResultId( performSaveOrUpdate( event ) );}}// 省略部分代碼... ...
}
如果上面的代碼中的customer對象是一個游離對象,那么當執行session.delete()方法時,會首先將游離的customer對象與session相關聯(轉換為持久態),然后再清理緩存時,再執行delete操作。 如果你想一次刪除多條數據,那么可以采用一個重載的delete()方法:delete("from Customer c where c.id > '8'");這個方法可以刪除符合條件的所有數據。