📄 sessionimpl.java
字号:
"not-null property references a null or transient value: ", persister.getClass(), persister.getPropertyNames()[i] ); } } } void removeCollection(CollectionPersister role, Serializable id) throws HibernateException { if ( log.isTraceEnabled() ) log.trace( "collection dereferenced while transient " + MessageHelper.infoString(role, id) ); /*if ( role.hasOrphanDelete() ) { throw new HibernateException( "You may not dereference a collection with cascade=\"all-delete-orphan\": " + MessageHelper.infoString(role, id) ); }*/ collectionRemovals.add( new ScheduledCollectionRemove(role, id, false, this) ); } static boolean isCollectionSnapshotValid(CollectionSnapshot snapshot) { return snapshot!=null && snapshot.getRole()!=null && snapshot.getKey()!=null; } static boolean isOwnerUnchanged(CollectionSnapshot snapshot, CollectionPersister persister, Serializable id) { return isCollectionSnapshotValid(snapshot) && persister.getRole().equals( snapshot.getRole() ) && id.equals( snapshot.getKey() ); } /** * Reattach a detached (disassociated) initialized or uninitialized * collection wrapper */ void reattachCollection(PersistentCollection collection, CollectionSnapshot snapshot) throws HibernateException { if ( collection.wasInitialized() ) { addInitializedDetachedCollection(collection, snapshot); } else { if ( !isCollectionSnapshotValid(snapshot) ) { throw new HibernateException("could not reassociate uninitialized transient collection"); } addUninitializedDetachedCollection( collection, getCollectionPersister( snapshot.getRole() ), snapshot.getKey() ); } } public void update(Object obj) throws HibernateException { if (obj==null) throw new NullPointerException("attempted to update null"); if ( reassociateIfUninitializedProxy(obj) ) return; Object object = unproxyAndReassociate(obj); ClassPersister persister = getPersister(object); if ( isEntryFor(object) ) { log.trace("object already associated with session"); // do nothing } else { // the object is transient Serializable id = persister.getIdentifier(object); if (id==null) { // assume this is a newly instantiated transient object throw new HibernateException( "The given object has a null identifier property " + MessageHelper.infoString(persister) ); } else { doUpdate(object, id, persister); } } } public void saveOrUpdate(Object obj) throws HibernateException { if (obj==null) throw new NullPointerException("attempted to update null"); if ( reassociateIfUninitializedProxy(obj) ) return; Object object = unproxyAndReassociate(obj); //a proxy is always "update", never "save" EntityEntry e = getEntry(object); if (e!=null && e.status!=DELETED) { // do nothing for persistent instances log.trace("saveOrUpdate() persistent instance"); } else if (e!=null) { //ie. e.status==DELETED log.trace("saveOrUpdate() deleted instance"); save(obj); } else { // the object is transient Boolean isUnsaved = interceptor.isUnsaved(object); ClassPersister persister = getPersister(object); if (isUnsaved==null) { // use unsaved-value if ( persister.isUnsaved(object) ) { log.trace("saveOrUpdate() unsaved instance"); save(obj); } else { Serializable id = persister.getIdentifier(object); if ( log.isTraceEnabled() ) log.trace("saveOrUpdate() previously saved instance with id: " + id); doUpdate(object, id, persister); } } else { if ( isUnsaved.booleanValue() ) { log.trace("saveOrUpdate() unsaved instance"); save(obj); } else { log.trace("saveOrUpdate() previously saved instance"); doUpdate( object, persister.getIdentifier(object), persister ); } } } } public void update(Object obj, Serializable id) throws HibernateException { if (id==null) throw new NullPointerException("null is not a valid identifier"); if (obj==null) throw new NullPointerException("attempted to update null"); if ( obj instanceof HibernateProxy ) { HibernateProxyHelper.getLazyInitializer( (HibernateProxy) obj ).setIdentifier(id); } if ( reassociateIfUninitializedProxy(obj) ) return; Object object = unproxyAndReassociate(obj); EntityEntry e = getEntry(object); if (e==null) { ClassPersister persister = getPersister(object); persister.setIdentifier(object, id); doUpdate(object, id, persister); } else { if ( !e.id.equals(id) ) throw new PersistentObjectException( "The instance passed to update() was already persistent: " + MessageHelper.infoString(e.persister, id) ); } } private void doUpdateMutable(Object object, Serializable id, ClassPersister persister) throws HibernateException { if ( log.isTraceEnabled() ) log.trace( "updating " + MessageHelper.infoString(persister, id) ); Key key = new Key(id, persister); checkUniqueness(key, object); if ( persister.implementsLifecycle() ) { log.debug("calling onUpdate()"); if ( ( (Lifecycle) object ).onUpdate(this) ) { // do callback log.debug("update vetoed by onUpdate()"); return; } } // this is a transient object with existing persistent state not loaded by the session new OnUpdateVisitor(this, id).process(object, persister); addEntity(key, object); addEntry(object, LOADED, null, id, persister.getVersion(object), LockMode.NONE, true, persister, false); } private void doUpdate(Object object, Serializable id, ClassPersister persister) throws HibernateException { if ( !persister.isMutable() ) { log.trace("immutable instance passed to doUpdate(), locking"); reassociate(object, id, persister); } else { doUpdateMutable(object, id, persister); } cascading++; try { Cascades.cascade(this, persister, object, Cascades.ACTION_SAVE_UPDATE, Cascades.CASCADE_ON_UPDATE); // do cascade } finally { cascading--; } } /** * Used only by replicate */ private void doReplicate(Object object, Serializable id, Object version, ReplicationMode replicationMode, ClassPersister persister) throws HibernateException { if ( log.isTraceEnabled() ) log.trace( "replicating changes to " + MessageHelper.infoString(persister, id) ); new OnReplicateVisitor(this, id).process(object, persister); Key key = new Key(id, persister); addEntity(key, object); addEntry(object, LOADED, null, id, version, LockMode.NONE, true, persister, true); cascading++; try { Cascades.cascade(this, persister, object, Cascades.ACTION_REPLICATE, Cascades.CASCADE_ON_UPDATE, replicationMode); // do cascade } finally { cascading--; } } private static final Object[] NO_ARGS = ArrayHelper.EMPTY_STRING_ARRAY; private static final Type[] NO_TYPES = ArrayHelper.EMPTY_TYPE_ARRAY; /** * Retrieve a list of persistent objects using a hibernate query */ public List find(String query) throws HibernateException { return find(query, NO_ARGS, NO_TYPES); } public List find(String query, Object value, Type type) throws HibernateException { return find( query, new Object[] { value }, new Type[] { type } ); } public List find(String query, Object[] values, Type[] types) throws HibernateException { return find(query, new QueryParameters(types, values) ); } public List find(String query, QueryParameters queryParameters) throws HibernateException { if ( log.isTraceEnabled() ) { log.trace( "find: " + query ); queryParameters.traceParameters(factory); } QueryTranslator[] q = getQueries(query, false); List results = Collections.EMPTY_LIST; dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called //execute the queries and return all result lists as a single list try { for ( int i=0; i<q.length; i++ ) { List currentResults; try { currentResults = q[i].list(this, queryParameters); } catch (SQLException sqle) { throw new JDBCException("Could not execute query", sqle); } currentResults.addAll(results); results = currentResults; } } finally { dontFlushFromFind--; } return results; } private QueryTranslator[] getQueries(String query, boolean scalar) throws HibernateException { // take the union of the query spaces (ie. the queried tables) QueryTranslator[] q = factory.getQuery(query, scalar); HashSet qs = new HashSet(); for ( int i=0; i<q.length; i++ ) { qs.addAll( q[i].getQuerySpaces() ); } autoFlushIfRequired(qs); return q; } public Iterator iterate(String query) throws HibernateException { return iterate(query, NO_ARGS, NO_TYPES); } public Iterator iterate(String query, Object value, Type type) throws HibernateException { return iterate( query, new Object[] { value }, new Type[] { type } ); } public Iterator iterate(String query, Object[] values, Type[] types) throws HibernateException { return iterate( query, new QueryParameters(types, values) ); } public Iterator iterate(String query, QueryParameters queryParameters) throws HibernateException { if ( log.isTraceEnabled() ) { log.trace( "iterate: " + query ); queryParameters.traceParameters(factory); } QueryTranslator[] q = getQueries(query, true); if (q.length==0) return Collections.EMPTY_LIST.iterator(); Iterator result = null; Iterator[] results = null; boolean many = q.length>1; if (many) results = new Iterator[q.length]; dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called try { //execute the queries and return all results as a single iterator for ( int i=0; i<q.length; i++ ) { try { result = q[i].iterate(queryParameters, this); } catch (SQLException sqle) { throw new JDBCException("Could not execute query", sqle); } if ( many ) { results[i] = result; } } return many ? new JoinedIterator(results) : result; } finally { dontFlushFromFind--; } } public ScrollableResults scroll(String query, QueryParameters queryParameters) throws HibernateException { if ( log.isTraceEnabled() ) { log.trace( "scroll: " + query ); queryParameters.traceParameters(factory); } QueryTranslator[] q = factory.getQuery(query, true); if (q.length!=1) throw new QueryException("implicit polymorphism not supported for scroll() queries"); autoFlushIfRequired( q[0].getQuerySpaces() ); dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called try { return q[0].scroll(queryParameters, this); } catch (SQLException sqle) { throw new JDBCException("Could not execute query", sqle); } finally { dontFlushFromFind--; } } public int delete(String query) throws HibernateException { return delete(query, NO_ARGS, NO_TYPES); } public int delete(String query, Object value, Type type) throws HibernateException { return delete( query, new Object[] { value }, new Type[] { type } ); } public int delete(String query, Object[] values, Type[] types) throws HibernateException { if ( log.isTraceEnabled() ) { log.trace( "delete: " + query ); if (values.length!=0) log.trace( "parameters: " + StringHelper.toString(values) ); } List list = find(query, values, types); int size = list.size(); for ( int i=0; i<size; i++ ) delete( list.get(i) ); return size; } private void checkUniqueness(Key key, Object object) throws HibernateException { Object entity = getEntity(key); if (entity==object) throw new AssertionFailure("object already associated in doSave()"); if (entity!=null) throw new NonUniqueObjectException( key.getIdentifier(), key.getMappedClass() ); } private EntityEntry reassociate(Object object, Serializable id, ClassPersister persister) throws HibernateException { if ( log.isTraceEnabled() ) log.trace( "reassociating transient instance: " + MessageHelper.infoString(persister, id) ); Key key = new Key(id, persister); checkUniqueness(key, object); addEntity(key, object); Object[] values = persister.getPropertyValues(object);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -