|
From: <one...@us...> - 2003-01-30 13:02:35
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/impl
In directory sc8-pr-cvs1:/tmp/cvs-serv19702/sf/hibernate/impl
Modified Files:
SessionImpl.java
Log Message:
more efficient collection update()s (keep a snapshot)
fixed a problem where null values in one-to-many lists and maps caused an exception
Index: SessionImpl.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/impl/SessionImpl.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** SessionImpl.java 28 Jan 2003 10:22:19 -0000 1.14
--- SessionImpl.java 30 Jan 2003 13:02:30 -0000 1.15
***************
*** 205,209 ****
* of a collection w.r.t its persistent state
*/
! static final class CollectionEntry implements Serializable {
boolean dirty;
transient boolean reached;
--- 205,209 ----
* of a collection w.r.t its persistent state
*/
! public static final class CollectionEntry implements CollectionSnapshot, Serializable {
boolean dirty;
transient boolean reached;
***************
*** 213,220 ****
transient boolean dorecreate;
boolean initialized;
! CollectionPersister currentPersister;
CollectionPersister loadedPersister;
! Serializable currentID;
! Serializable loadedID;
Serializable snapshot; // session-start/post-flush persistent state
CollectionEntry() {
--- 213,220 ----
transient boolean dorecreate;
boolean initialized;
! transient CollectionPersister currentPersister;
CollectionPersister loadedPersister;
! transient Serializable currentKey;
! Serializable loadedKey;
Serializable snapshot; // session-start/post-flush persistent state
CollectionEntry() {
***************
*** 225,231 ****
this.dirty = false;
this.initialized = initialized;
! this.loadedID = loadedID;
this.loadedPersister = loadedPersister;
}
//default behavior; will be overridden in deep lazy collections
boolean isDirty(PersistentCollection coll) throws HibernateException {
--- 225,238 ----
this.dirty = false;
this.initialized = initialized;
! this.loadedKey = loadedID;
this.loadedPersister = loadedPersister;
}
+ CollectionEntry(CollectionSnapshot cs, SessionFactoryImplementor factory) throws MappingException {
+ this.dirty = cs.getDirty();
+ this.snapshot = cs.getSnapshot();
+ this.loadedKey = cs.getLoadedKey();
+ this.loadedPersister = factory.getCollectionPersister( cs.getLoadedPersisterRole() );
+ this.initialized = true;
+ }
//default behavior; will be overridden in deep lazy collections
boolean isDirty(PersistentCollection coll) throws HibernateException {
***************
*** 246,250 ****
if ( log.isDebugEnabled() && dirty && loadedPersister!=null ) log.debug(
! "Collection dirty: " + infoString(loadedPersister, loadedID)
);
--- 253,257 ----
if ( log.isDebugEnabled() && dirty && loadedPersister!=null ) log.debug(
! "Collection dirty: " + infoString(loadedPersister, loadedKey)
);
***************
*** 262,266 ****
if (!processed) throw new AssertionFailure("Hibernate has a bug processing collections");
! loadedID = currentID;
loadedPersister = currentPersister;
dirty = false;
--- 269,273 ----
if (!processed) throw new AssertionFailure("Hibernate has a bug processing collections");
! loadedKey = currentKey;
loadedPersister = currentPersister;
dirty = false;
***************
*** 269,272 ****
--- 276,295 ----
}
}
+ public boolean getDirty() {
+ return dirty;
+ }
+ public Serializable getLoadedKey() {
+ return loadedKey;
+ }
+ public String getLoadedPersisterRole() {
+ return loadedPersister.getRole();
+ }
+ public Serializable getSnapshot() {
+ return snapshot;
+ }
+ public void setDirty() {
+ dirty = true;
+ }
+
}
***************
*** 475,479 ****
//add a new collection (ie. an initialized one)
private void addCollectionEntry(PersistentCollection collection) {
! collections.put( collection, new CollectionEntry() );
}
--- 498,504 ----
//add a new collection (ie. an initialized one)
private void addCollectionEntry(PersistentCollection collection) {
! CollectionEntry ce = new CollectionEntry();
! collections.put(collection, ce);
! collection.setCollectionSnapshot(ce);
}
***************
*** 919,934 ****
}
//TODO: rename this method
private void removeCollectionsFor(Type type, Serializable id, Object value) throws HibernateException {
if ( type.isPersistentCollectionType() ) {
! CollectionPersister role = getCollectionPersister( ( (PersistentCollectionType) type ).getRole() );
! if ( value!=null && (value instanceof PersistentCollection) && !( (PersistentCollection) value ).wasInitialized() ) {
PersistentCollection coll = (PersistentCollection) value;
! if ( coll.setSession(this) ) {
! addUninitializedCollection(coll, role, id);
}
}
else {
! collectionRemovals.add( new ScheduledCollectionRemove(role, id, this) );
}
}
--- 944,974 ----
}
+ private void removeCollection(CollectionPersister role, Serializable id) {
+ collectionRemovals.add( new ScheduledCollectionRemove(role, id, this) );
+ }
+
//TODO: rename this method
private void removeCollectionsFor(Type type, Serializable id, Object value) throws HibernateException {
if ( type.isPersistentCollectionType() ) {
! CollectionPersister persister = getCollectionPersister( ( (PersistentCollectionType) type ).getRole() );
! if ( value!=null && (value instanceof PersistentCollection) ) {
PersistentCollection coll = (PersistentCollection) value;
! if ( coll.wasInitialized() ) {
! CollectionSnapshot cs = coll.getCollectionSnapshot();
! if (cs!=null && cs.getLoadedPersisterRole().equals( persister.getRole() ) && cs.getLoadedKey().equals(id) ) {
! if ( coll.setSession(this) ) addInitializedCollection(coll, cs);
! }
! else {
! removeCollection(persister, id);
! }
! }
! else {
! if ( coll.setSession(this) ) {
! addUninitializedCollection(coll, persister, id);
! }
}
}
else {
! removeCollection(persister, id);
}
}
***************
*** 2058,2064 ****
//TODO: move this to the entry
! if ( ce.dorecreate ) collectionCreations.add( new ScheduledCollectionRecreate(coll, ce.currentPersister, ce.currentID, this) );
! if ( ce.doremove ) collectionRemovals.add( new ScheduledCollectionRemove(ce.loadedPersister, ce.loadedID, this) );
! if ( ce.doupdate ) collectionUpdates.add( new ScheduledCollectionUpdate(coll, ce.loadedPersister, ce.loadedID, this) );
}
--- 2098,2104 ----
//TODO: move this to the entry
! if ( ce.dorecreate ) collectionCreations.add( new ScheduledCollectionRecreate(coll, ce.currentPersister, ce.currentKey, this) );
! if ( ce.doremove ) collectionRemovals.add( new ScheduledCollectionRemove(ce.loadedPersister, ce.loadedKey, this) );
! if ( ce.doupdate ) collectionUpdates.add( new ScheduledCollectionUpdate(coll, ce.loadedPersister, ce.loadedKey, this) );
}
***************
*** 2179,2188 ****
CollectionPersister persister = getCollectionPersister( ( (PersistentCollectionType) type).getRole() );
ce.currentPersister = persister;
! ce.currentID = getEntityIdentifier(owner);
if ( log.isDebugEnabled() ) {
log.debug(
! "Collection found: " + infoString(persister, ce.currentID) +
! ", was: " + infoString(ce.loadedPersister, ce.loadedID)
);
}
--- 2219,2228 ----
CollectionPersister persister = getCollectionPersister( ( (PersistentCollectionType) type).getRole() );
ce.currentPersister = persister;
! ce.currentKey = getEntityIdentifier(owner);
if ( log.isDebugEnabled() ) {
log.debug(
! "Collection found: " + infoString(persister, ce.currentKey) +
! ", was: " + infoString(ce.loadedPersister, ce.loadedKey)
);
}
***************
*** 2231,2238 ****
CollectionEntry entry = getCollectionEntry(coll);
if ( log.isDebugEnabled() && entry.loadedPersister!=null ) log.debug(
! "Collection dereferenced: " + infoString(entry.loadedPersister, entry.loadedID)
);
entry.currentPersister=null;
! entry.currentID=null;
prepareCollectionForUpdate(coll, entry);
--- 2271,2278 ----
CollectionEntry entry = getCollectionEntry(coll);
if ( log.isDebugEnabled() && entry.loadedPersister!=null ) log.debug(
! "Collection dereferenced: " + infoString(entry.loadedPersister, entry.loadedKey)
);
entry.currentPersister=null;
! entry.currentKey=null;
prepareCollectionForUpdate(coll, entry);
***************
*** 2250,2254 ****
if (
entry.loadedPersister!=entry.currentPersister || // if either its role changed,
! !entry.currentPersister.getKeyType().equals(entry.loadedID, entry.currentID) // or its key changed (for nested collections)
) {
--- 2290,2294 ----
if (
entry.loadedPersister!=entry.currentPersister || // if either its role changed,
! !entry.currentPersister.getKeyType().equals(entry.loadedKey, entry.currentKey) // or its key changed (for nested collections)
) {
***************
*** 2339,2343 ****
// add a collection we just loaded up (still needs initializing)
public void addUninitializedCollection(PersistentCollection collection, CollectionPersister persister, Serializable id) {
! collections.put( collection, new CollectionEntry(persister, id, false) );
}
--- 2379,2385 ----
// add a collection we just loaded up (still needs initializing)
public void addUninitializedCollection(PersistentCollection collection, CollectionPersister persister, Serializable id) {
! CollectionEntry ce = new CollectionEntry(persister, id, false);
! collections.put(collection, ce);
! collection.setCollectionSnapshot(ce);
}
***************
*** 2347,2350 ****
--- 2389,2399 ----
ce.postInitialize(collection);
collections.put(collection, ce);
+ collection.setCollectionSnapshot(ce);
+ }
+
+ private void addInitializedCollection(PersistentCollection collection, CollectionSnapshot cs) throws HibernateException {
+ CollectionEntry ce = new CollectionEntry(cs, factory);
+ collections.put(collection, ce);
+ collection.setCollectionSnapshot(ce);
}
***************
*** 2370,2374 ****
public Serializable getLoadedCollectionKey(PersistentCollection coll) {
! return getCollectionEntry(coll).loadedID;
}
--- 2419,2423 ----
public Serializable getLoadedCollectionKey(PersistentCollection coll) {
! return getCollectionEntry(coll).loadedKey;
}
***************
*** 2386,2393 ****
//if (closed) throw new LazyInitializationException( "Hibernate failed trying to load a collection - the session is closed" );
! if ( log.isTraceEnabled() ) log.trace( "initializing collection " + infoString(ce.loadedPersister, ce.loadedID) );
CollectionPersister persister = ce.loadedPersister;
! Serializable id = ce.loadedID;
Object owner = getEntity( new Key( id, getPersister( persister.getOwnerClass() ) ) );
--- 2435,2442 ----
//if (closed) throw new LazyInitializationException( "Hibernate failed trying to load a collection - the session is closed" );
! if ( log.isTraceEnabled() ) log.trace( "initializing collection " + infoString(ce.loadedPersister, ce.loadedKey) );
CollectionPersister persister = ce.loadedPersister;
! Serializable id = ce.loadedKey;
Object owner = getEntity( new Key( id, getPersister( persister.getOwnerClass() ) ) );
***************
*** 2551,2555 ****
}
! values[0] = e.loadedID;
types[0] = e.loadedPersister.getKeyType();
--- 2600,2604 ----
}
! values[0] = e.loadedKey;
types[0] = e.loadedPersister.getKeyType();
|