📄 sessionimpl.java
字号:
public Object load(Class entityClass, Serializable id) throws HibernateException { return load( entityClass.getName(), id ); } public Object load(String entityName, Serializable id) throws HibernateException { LoadEvent event = new LoadEvent(id, entityName, false, this); boolean success = false; try { fireLoad( event, LoadEventListener.LOAD ); if ( event.getResult() == null ) { getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id ); } success = true; return event.getResult(); } finally { afterOperation(success); } } public Object get(Class entityClass, Serializable id) throws HibernateException { return get( entityClass.getName(), id ); } public Object get(String entityName, Serializable id) throws HibernateException { LoadEvent event = new LoadEvent(id, entityName, false, this); boolean success = false; try { fireLoad(event, LoadEventListener.GET); success = true; return event.getResult(); } finally { afterOperation(success); } } /** * Load the data for the object with the specified id into a newly created object. * This is only called when lazily initializing a proxy. * Do NOT return a proxy. */ public Object immediateLoad(String entityName, Serializable id) throws HibernateException { if ( log.isDebugEnabled() ) { EntityPersister persister = getFactory().getEntityPersister(entityName); log.debug( "initializing proxy: " + MessageHelper.infoString( persister, id, getFactory() ) ); } LoadEvent event = new LoadEvent(id, entityName, true, this); fireLoad(event, LoadEventListener.IMMEDIATE_LOAD); return event.getResult(); } public Object internalLoad(String entityName, Serializable id, boolean eager, boolean nullable) throws HibernateException { // todo : remove LoadEventListener.LoadType type = nullable ? LoadEventListener.INTERNAL_LOAD_NULLABLE : eager ? LoadEventListener.INTERNAL_LOAD_EAGER : LoadEventListener.INTERNAL_LOAD_LAZY; LoadEvent event = new LoadEvent(id, entityName, true, this); fireLoad(event, type); if ( !nullable ) { UnresolvableObjectException.throwIfNull( event.getResult(), id, entityName ); } return event.getResult(); } public Object load(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException { return load( entityClass.getName(), id, lockMode ); } public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException { LoadEvent event = new LoadEvent(id, entityName, lockMode, this); fireLoad( event, LoadEventListener.LOAD ); return event.getResult(); } public Object get(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException { return get( entityClass.getName(), id, lockMode ); } public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException { LoadEvent event = new LoadEvent(id, entityName, lockMode, this); fireLoad(event, LoadEventListener.GET); return event.getResult(); } private void fireLoad(LoadEvent event, LoadType loadType) { errorIfClosed(); checkTransactionSynchStatus(); LoadEventListener[] loadEventListener = listeners.getLoadEventListeners(); for ( int i = 0; i < loadEventListener.length; i++ ) { loadEventListener[i].onLoad(event, loadType); } } // refresh() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public void refresh(Object object) throws HibernateException { fireRefresh( new RefreshEvent(object, this) ); } public void refresh(Object object, LockMode lockMode) throws HibernateException { fireRefresh( new RefreshEvent(object, lockMode, this) ); } public void refresh(Object object, Map refreshedAlready) throws HibernateException { fireRefresh( refreshedAlready, new RefreshEvent(object, this) ); } private void fireRefresh(RefreshEvent refreshEvent) { errorIfClosed(); checkTransactionSynchStatus(); RefreshEventListener[] refreshEventListener = listeners.getRefreshEventListeners(); for ( int i = 0; i < refreshEventListener.length; i++ ) { refreshEventListener[i].onRefresh( refreshEvent ); } } private void fireRefresh(Map refreshedAlready, RefreshEvent refreshEvent) { errorIfClosed(); checkTransactionSynchStatus(); RefreshEventListener[] refreshEventListener = listeners.getRefreshEventListeners(); for ( int i = 0; i < refreshEventListener.length; i++ ) { refreshEventListener[i].onRefresh( refreshEvent, refreshedAlready ); } } // replicate() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public void replicate(Object obj, ReplicationMode replicationMode) throws HibernateException { fireReplicate( new ReplicateEvent(obj, replicationMode, this) ); } public void replicate(String entityName, Object obj, ReplicationMode replicationMode) throws HibernateException { fireReplicate( new ReplicateEvent(entityName, obj, replicationMode, this) ); } private void fireReplicate(ReplicateEvent event) { errorIfClosed(); checkTransactionSynchStatus(); ReplicateEventListener[] replicateEventListener = listeners.getReplicateEventListeners(); for ( int i = 0; i < replicateEventListener.length; i++ ) { replicateEventListener[i].onReplicate(event); } } // evict() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * remove any hard references to the entity that are held by the infrastructure * (references held by application or other persistant instances are okay) */ public void evict(Object object) throws HibernateException { fireEvict( new EvictEvent(object, this) ); } private void fireEvict(EvictEvent evictEvent) { errorIfClosed(); checkTransactionSynchStatus(); EvictEventListener[] evictEventListener = listeners.getEvictEventListeners(); for ( int i = 0; i < evictEventListener.length; i++ ) { evictEventListener[i].onEvict( evictEvent ); } } /** * detect in-memory changes, determine if the changes are to tables * named in the query and, if so, complete execution the flush */ protected boolean autoFlushIfRequired(Set querySpaces) throws HibernateException { errorIfClosed(); if ( ! isTransactionInProgress() ) { // do not auto-flush while outside a transaction return false; } AutoFlushEvent event = new AutoFlushEvent(querySpaces, this); AutoFlushEventListener[] autoFlushEventListener = listeners.getAutoFlushEventListeners(); for ( int i = 0; i < autoFlushEventListener.length; i++ ) { autoFlushEventListener[i].onAutoFlush(event); } return event.isFlushRequired(); } public boolean isDirty() throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); log.debug("checking session dirtiness"); if ( actionQueue.areInsertionsOrDeletionsQueued() ) { log.debug("session dirty (scheduled updates and insertions)"); return true; } else { DirtyCheckEvent event = new DirtyCheckEvent(this); DirtyCheckEventListener[] dirtyCheckEventListener = listeners.getDirtyCheckEventListeners(); for ( int i = 0; i < dirtyCheckEventListener.length; i++ ) { dirtyCheckEventListener[i].onDirtyCheck(event); } return event.isDirty(); } } public void flush() throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); if ( persistenceContext.getCascadeLevel() > 0 ) { throw new HibernateException("Flush during cascade is dangerous"); } FlushEventListener[] flushEventListener = listeners.getFlushEventListeners(); for ( int i = 0; i < flushEventListener.length; i++ ) { flushEventListener[i].onFlush( new FlushEvent(this) ); } } public void forceFlush(EntityEntry entityEntry) throws HibernateException { errorIfClosed(); if ( log.isDebugEnabled() ) { log.debug( "flushing to force deletion of re-saved object: " + MessageHelper.infoString( entityEntry.getPersister(), entityEntry.getId(), getFactory() ) ); } if ( persistenceContext.getCascadeLevel() > 0 ) { throw new ObjectDeletedException( "deleted object would be re-saved by cascade (remove deleted object from associations)", entityEntry.getId(), entityEntry.getPersister().getEntityName() ); } flush(); } public Filter getEnabledFilter(String filterName) { checkTransactionSynchStatus(); return (Filter) enabledFilters.get(filterName); } public Filter enableFilter(String filterName) { errorIfClosed(); checkTransactionSynchStatus(); FilterImpl filter = new FilterImpl( factory.getFilterDefinition(filterName) ); enabledFilters.put(filterName, filter); return filter; } public void disableFilter(String filterName) { errorIfClosed(); checkTransactionSynchStatus(); enabledFilters.remove(filterName); } public Object getFilterParameterValue(String filterParameterName) { errorIfClosed(); checkTransactionSynchStatus(); String[] parsed = parseFilterParameterName(filterParameterName); FilterImpl filter = (FilterImpl) enabledFilters.get( parsed[0] ); if (filter == null) { throw new IllegalArgumentException("Filter [" + parsed[0] + "] currently not enabled"); } return filter.getParameter( parsed[1] ); } public Type getFilterParameterType(String filterParameterName) { errorIfClosed(); checkTransactionSynchStatus(); String[] parsed = parseFilterParameterName(filterParameterName); FilterDefinition filterDef = factory.getFilterDefinition( parsed[0] ); if (filterDef == null) { throw new IllegalArgumentException("Filter [" + parsed[0] + "] not defined"); } Type type = filterDef.getParameterType( parsed[1] ); if (type == null) { // this is an internal error of some sort... throw new InternalError("Unable to locate type for filter parameter"); } return type; } public Map getEnabledFilters() { errorIfClosed(); checkTransactionSynchStatus(); // First, validate all the enabled filters... //TODO: this implementation has bad performance Iterator itr = enabledFilters.values().iterator(); while ( itr.hasNext() ) { final Filter filter = (Filter) itr.next(); filter.validate(); } return enabledFilters; } private String[] parseFilterParameterName(String filterParameterName) { int dot = filterParameterName.indexOf('.'); if (dot <= 0) { throw new IllegalArgumentException("Invalid filter-parameter name format"); // TODO: what type? } String filterName = filterParameterName.substring(0, dot); String parameterName = filterParameterName.substring(dot+1); return new String[] {filterName, parameterName}; } /** * Retrieve a list of persistent objects using a hibernate query */ public List find(String query) throws HibernateException { return list( query, new QueryParameters() ); } public List find(String query, Object value, Type type) throws HibernateException { return list( query, new QueryParameters(type, value) ); } public List find(String query, Object[] values, Type[] types) throws HibernateException { return list( query, new QueryParameters(types, values) ); } public List list(String query, QueryParameters queryParameters) throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); queryParameters.validateParameters(); HQLQueryPlan plan = getHQLQueryPlan( query, false ); autoFlushIfRequired( plan.getQuerySpaces() ); List results = CollectionHelper.EMPTY_LIST; boolean success = false; dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called try { results = plan.performList( queryParameters, this ); success = true; } finally { dontFlushFromFind--; afterOperation(success); } return results; } public int executeUpdate(String query, QueryParameters queryParameters) throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); queryParameters.validateParameters(); HQLQueryPlan plan = getHQLQueryPlan( query, false ); autoFlushIfRequired( plan.getQuerySpaces() ); boolean success = false; int result = 0; try { result = plan.performExecuteUpdate( queryParameters, this ); success = true; } finally { afterOperation(success); } return result; } public int executeNativeUpdate(NativeSQLQuerySpecification nativeQuerySpecification, QueryParameters queryParameters) throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); queryParameters.validateParameters(); NativeSQLQueryPlan plan = getNativeSQLQueryPlan(nativeQuerySpecification); autoFlushIfRequired( plan.getCustomQuery().getQuerySpaces() ); boolean success = false; int result = 0; try { result = plan.performExecuteUpdate(queryParameters, this); success = true; } finally { afterOperation(success); } return result; } public Iterator iterate(String query) throws HibernateException { return iterate( query, new QueryParameters() ); } public Iterator iterate(String query, Object value, Type type) throws HibernateException { return iterate( query, new QueryParameters(type, value) ); } public Iterator iterate(String query, Object[] values, Type[] types) throws HibernateException { return iterate( query, new QueryParameters(types, values) ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -