FileDocCategorySizeDatePackage
PersistentIdentifierBag.javaAPI DocHibernate 3.2.510754Mon Nov 06 15:00:42 GMT 2006org.hibernate.collection

PersistentIdentifierBag

public class PersistentIdentifierBag extends AbstractPersistentCollection implements List
An IdentifierBag implements "bag" semantics more efficiently than a regular Bag by adding a synthetic identifier column to the table. This identifier is unique for all rows in the table, allowing very efficient updates and deletes. The value of the identifier is never exposed to the application.

IdentifierBags may not be used for a many-to-one association. Furthermore, there is no reason to use inverse="true".
author
Gavin King

Fields Summary
protected List
values
protected Map
identifiers
Constructors Summary
public PersistentIdentifierBag(org.hibernate.engine.SessionImplementor session)

		super(session);
	
public PersistentIdentifierBag()

public PersistentIdentifierBag(org.hibernate.engine.SessionImplementor session, Collection coll)

		super(session);
		if (coll instanceof List) {
			values = (List) coll;
		}
		else {
			values = new ArrayList();
			Iterator iter = coll.iterator();
			while ( iter.hasNext() ) {
				values.add( iter.next() );
			}
		}
		setInitialized();
		setDirectlyAccessible(true);
		identifiers = new HashMap();
	
Methods Summary
public voidadd(int index, java.lang.Object element)

		write();
		beforeAdd(index);
		values.add(index, element);
	
public booleanadd(java.lang.Object o)

		write();
		values.add(o);
		return true;
	
public booleanaddAll(int index, java.util.Collection c)

		if ( c.size() > 0 ) {
			Iterator iter = c.iterator();
			while ( iter.hasNext() ) {
				add( index++, iter.next() );
			}
			return true;
		}
		else {
			return false;
		}
	
public booleanaddAll(java.util.Collection c)

		if ( c.size()> 0 ) {
			write();
			return values.addAll(c);
		}
		else {
			return false;
		}
	
public voidafterRowInsert(org.hibernate.persister.collection.CollectionPersister persister, java.lang.Object entry, int i)

		//TODO: if we are using identity columns, fetch the identifier
	
private voidbeforeAdd(int index)

		for ( int i=index; i<values.size(); i++ ) {
			identifiers.put( new Integer(i+1), identifiers.get( new Integer(i) ) );
		}
		identifiers.remove( new Integer(index) );
	
public voidbeforeInitialize(org.hibernate.persister.collection.CollectionPersister persister, int anticipatedSize)

		identifiers = anticipatedSize <= 0 ? new HashMap() : new HashMap( anticipatedSize + 1 + (int)( anticipatedSize * .75f ), .75f );
		values = anticipatedSize <= 0 ? new ArrayList() : new ArrayList( anticipatedSize );
	
private voidbeforeRemove(int index)

		Object removedId = identifiers.get( new Integer(index) );
		int last = values.size()-1;
		for ( int i=index; i<last; i++ ) {
			Object id = identifiers.get( new Integer(i+1) );
	        if ( id==null ) {
				identifiers.remove( new Integer(i) );
	        }
	        else {
				identifiers.put( new Integer(i), id );
	        }
		}
		identifiers.put( new Integer(last), removedId );
	
public voidclear()

		initialize( true );
		if ( ! values.isEmpty() || ! identifiers.isEmpty() ) {
			values.clear();
			identifiers.clear();
			dirty();
		}
	
public booleancontains(java.lang.Object o)

		read();
		return values.contains(o);
	
public booleancontainsAll(java.util.Collection c)

		read();
		return values.containsAll(c);
	
public java.io.Serializabledisassemble(org.hibernate.persister.collection.CollectionPersister persister)

		Serializable[] result = new Serializable[ values.size() * 2 ];
		int i=0;
		for (int j=0; j< values.size(); j++) {
			Object value = values.get(j);
			result[i++] = persister.getIdentifierType().disassemble( identifiers.get( new Integer(j) ), getSession(), null );
			result[i++] = persister.getElementType().disassemble( value, getSession(), null );
		}
		return result;
	
public booleanempty()

		return values.isEmpty();
	
public java.util.Iteratorentries(org.hibernate.persister.collection.CollectionPersister persister)

		return values.iterator();
	
public booleanentryExists(java.lang.Object entry, int i)

		return entry!=null;
	
public booleanequalsSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		Type elementType = persister.getElementType();
		Map snap = (Map) getSnapshot();
		if ( snap.size()!= values.size() ) return false;
		for ( int i=0; i<values.size(); i++ ) {
			Object value = values.get(i);
			Object id = identifiers.get( new Integer(i) );
			if (id==null) return false;
			Object old = snap.get(id);
			if ( elementType.isDirty( old, value, getSession() ) ) return false;
		}
		return true;
	
public java.lang.Objectget(int index)

		read();
		return values.get(index);
	
public java.util.IteratorgetDeletes(org.hibernate.persister.collection.CollectionPersister persister, boolean indexIsFormula)

		Map snap = (Map) getSnapshot();
		List deletes = new ArrayList( snap.keySet() );
		for ( int i=0; i<values.size(); i++ ) {
			if ( values.get(i)!=null ) deletes.remove( identifiers.get( new Integer(i) ) );
		}
		return deletes.iterator();
	
public java.lang.ObjectgetElement(java.lang.Object entry)

		return entry;
	
public java.lang.ObjectgetIdentifier(java.lang.Object entry, int i)

		return identifiers.get( new Integer(i) );
	
public java.lang.ObjectgetIndex(java.lang.Object entry, int i, org.hibernate.persister.collection.CollectionPersister persister)

		throw new UnsupportedOperationException("Bags don't have indexes");
	
public java.util.CollectiongetOrphans(java.io.Serializable snapshot, java.lang.String entityName)

		Map sn = (Map) snapshot;
		return getOrphans( sn.values(), values, entityName, getSession() );
	
public java.io.SerializablegetSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		
		EntityMode entityMode = getSession().getEntityMode();
		
		HashMap map = new HashMap( values.size() );
		Iterator iter = values.iterator();
		int i=0;
		while ( iter.hasNext() ) {
			Object value = iter.next();
			map.put(
				identifiers.get( new Integer(i++) ),
				persister.getElementType().deepCopy(value, entityMode, persister.getFactory())
			);
		}
		return map;
	
public java.lang.ObjectgetSnapshotElement(java.lang.Object entry, int i)

		Map snap = (Map) getSnapshot();
		Object id = identifiers.get( new Integer(i) );
		return snap.get(id);
	
public intindexOf(java.lang.Object o)

		read();
		return values.indexOf(o);
	
public voidinitializeFromCache(org.hibernate.persister.collection.CollectionPersister persister, java.io.Serializable disassembled, java.lang.Object owner)

		Serializable[] array = (Serializable[]) disassembled;
		int size = array.length;
		beforeInitialize( persister, size );
		for ( int i = 0; i < size; i+=2 ) {
			identifiers.put(
				new Integer(i/2),
				persister.getIdentifierType().assemble( array[i], getSession(), owner )
			);
			values.add( persister.getElementType().assemble( array[i+1], getSession(), owner ) );
		}
	
public booleanisEmpty()

		return readSize() ? getCachedSize()==0 : values.isEmpty();
	
public booleanisSnapshotEmpty(java.io.Serializable snapshot)

		return ( (Map) snapshot ).isEmpty();
	
public booleanisWrapper(java.lang.Object collection)

		return values==collection;
	
public java.util.Iteratoriterator()

		read();
		return new IteratorProxy( values.iterator() );
	
public intlastIndexOf(java.lang.Object o)

		read();
		return values.lastIndexOf(o);
	
public java.util.ListIteratorlistIterator()

		read();
		return new ListIteratorProxy( values.listIterator() );
	
public java.util.ListIteratorlistIterator(int index)

		read();
		return new ListIteratorProxy( values.listIterator(index) );
	
public booleanneedsInserting(java.lang.Object entry, int i, org.hibernate.type.Type elemType)


		Map snap = (Map) getSnapshot();
		Object id = identifiers.get( new Integer(i) );
		return entry!=null && ( id==null || snap.get(id)==null );
	
public booleanneedsUpdating(java.lang.Object entry, int i, org.hibernate.type.Type elemType)


		if (entry==null) return false;
		Map snap = (Map) getSnapshot();
		Object id = identifiers.get( new Integer(i) );
		if (id==null) return false;
		Object old = snap.get(id);
		return old!=null && elemType.isDirty( old, entry, getSession() );
	
public voidpreInsert(org.hibernate.persister.collection.CollectionPersister persister)

		Iterator iter = values.iterator();
		int i=0;
		while ( iter.hasNext() ) {
			Object entry = iter.next();
			Integer loc = new Integer(i++);
			if ( !identifiers.containsKey(loc) ) { //TODO: native ids
				Serializable id = persister.getIdentifierGenerator().generate( getSession(), entry );
				identifiers.put(loc, id);
			}
		}
	
public java.lang.ObjectreadFrom(java.sql.ResultSet rs, org.hibernate.persister.collection.CollectionPersister persister, org.hibernate.loader.CollectionAliases descriptor, java.lang.Object owner)


		Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
		Object old = identifiers.put(
			new Integer( values.size() ),
			persister.readIdentifier( rs, descriptor.getSuffixedIdentifierAlias(), getSession() )
		);
		if ( old==null ) values.add(element); //maintain correct duplication if loaded in a cartesian product
		return element;
	
public booleanremove(java.lang.Object o)

		initialize( true );
		int index = values.indexOf(o);
		if (index>=0) {
			beforeRemove(index);
			values.remove(index);
			dirty();
			return true;
		}
		else {
			return false;
		}
	
public java.lang.Objectremove(int index)

		write();
		beforeRemove(index);
		return values.remove(index);
	
public booleanremoveAll(java.util.Collection c)

		if ( c.size() > 0 ) {
			boolean result = false;
			Iterator iter = c.iterator();
			while ( iter.hasNext() ) {
				if ( remove( iter.next() ) ) result=true;
			}
			return result;
		}
		else {
			return false;
		}
	
public booleanretainAll(java.util.Collection c)

		initialize( true );
		if ( values.retainAll( c ) ) {
			dirty();
			return true;
		}
		else {
			return false;
		}
	
public java.lang.Objectset(int index, java.lang.Object element)

		write();
		return values.set(index, element);
	
public intsize()

		return readSize() ? getCachedSize() : values.size();
	
public java.util.ListsubList(int fromIndex, int toIndex)

		read();
		return new ListProxy( values.subList(fromIndex, toIndex) );
	
public java.lang.Object[]toArray()

		read();
		return values.toArray();
	
public java.lang.Object[]toArray(java.lang.Object[] a)

		read();
		return values.toArray(a);