public AssociationPersister build() { return new AssociationPersister( this ); } }
private void addNavigationalInformationForInverseSide(int propertyIndex, AssociationKeyMetadata associationKeyMetadata, Object[] newColumnValue) { RowKey rowKey = getInverseRowKey( associationKeyMetadata, newColumnValue ); Tuple associationRow = new Tuple(); for ( String column : rowKey.getColumnNames() ) { associationRow.put( column, rowKey.getColumnValue( column ) ); } AssociationPersister associationPersister = createInverseAssociationPersister( propertyIndex, associationKeyMetadata, newColumnValue ); associationPersister.getAssociation().put( rowKey, associationRow ); if ( associationPersister.getAssociationContext().getEntityTuplePointer().getTuple() == null ) { throw log.entityTupleNotFound( associationKeyMetadata.getCollectionRole(), associationPersister.getAssociationKey().getEntityKey() ); } associationPersister.flushToDatastore(); }
/** * Reads the entity hosting the association from the datastore and applies any property changes from the server * side. */ private void updateHostingEntityIfRequired() { if ( hostingEntity != null && hostingEntityRequiresReadAfterUpdate() ) { OgmEntityPersister entityPersister = getHostingEntityPersister(); if ( GridDialects.hasFacet( gridDialect, GroupingByEntityDialect.class ) ) { ( (GroupingByEntityDialect) gridDialect ).flushPendingOperations( getAssociationKey().getEntityKey(), entityPersister.getTupleContext( session ) ); } entityPersister.processUpdateGeneratedProperties( entityPersister.getIdentifier( hostingEntity, session ), hostingEntity, new Object[entityPersister.getPropertyNames().length], session ); } }
/** * Writes out the changes gathered in the {@link Association} managed by this persister to the datastore. */ public void flushToDatastore() { if ( getAssociation().isEmpty() ) { gridDialect.removeAssociation( getAssociationKey(), getAssociationContext() ); association = null; OgmEntityEntryState.getStateFor( session, hostingEntity ).setAssociation( associationKeyMetadata.getCollectionRole(), null ); } else if ( !getAssociation().getOperations().isEmpty() ) { gridDialect.insertOrUpdateAssociation( getAssociationKey(), getAssociation(), getAssociationContext() ); } updateHostingEntityIfRequired(); }
public AssociationKey getAssociationKey() { if ( associationKey == null ) { final Object[] columnValues = getKeyColumnValues(); EntityKey ownerEntityKey; if ( key != null ) { ownerEntityKey = EntityKeyBuilder.fromPersister( (OgmEntityPersister) getHostingEntityPersister(), (Serializable) key, session ); } else { ownerEntityKey = new EntityKey( ( (OgmEntityPersister) getHostingEntityPersister() ).getEntityKeyMetadata(), columnValues ); } associationKey = new AssociationKey( associationKeyMetadata, columnValues, ownerEntityKey ); } return associationKey; }
Tuple assocEntryTuple = associationPersister.getAssociation().get( assocEntryKey ); if ( assocEntryTuple == null ) { throw new AssertionFailure( "Updating a collection tuple that is not present: " + "table {" + getTableName() + "} collectionKey {" + key + "} entry {" + entry + "}" ); updateInverseSideOfAssociationNavigation( session, entry, associationPersister.getAssociationKey(), assocEntryTuple, Action.REMOVE, assocEntryKey ); associationPersister.getAssociation().put( assocEntryKey, assocEntryTuple ); updateInverseSideOfAssociationNavigation( session, entry, associationPersister.getAssociationKey(), assocEntryTuple, Action.ADD, assocEntryKey ); associationPersister.flushToDatastore();
Association association = associationPersister.getAssociationOrNull(); session, entity, associationPersister.getAssociationKey(), associationRow, Action.REMOVE, associationPersister.flushToDatastore();
public Association getAssociation() { if ( association == null ) { AssociationKey key = getAssociationKey(); if ( hostingEntity != null ) { OgmEntityEntryState entryState = OgmEntityEntryState.getStateFor( session, hostingEntity ); if ( entryState.hasAssociation( associationKeyMetadata.getCollectionRole() ) ) { association = entryState.getAssociation( associationKeyMetadata.getCollectionRole() ); } else { association = gridDialect.getAssociation( key, getAssociationContext() ); OgmEntityEntryState.getStateFor( session, hostingEntity ) .setAssociation( associationKeyMetadata.getCollectionRole(), association ); } } else { association = gridDialect.getAssociation( key, getAssociationContext() ); } if ( association == null ) { association = gridDialect.createAssociation( key, getAssociationContext() ); if ( hostingEntity != null ) { OgmEntityEntryState.getStateFor( session, hostingEntity ) .setAssociation( associationKeyMetadata.getCollectionRole(), association ); } } } return association; }
private void removeNavigationalInformationFromInverseSide(int propertyIndex, AssociationKeyMetadata associationKeyMetadata, Object[] oldColumnValue) { // If the association involves entities deleted by a previous operation of the current batch, // it does not make sense trying to update association itself EntityKey entityKey = new EntityKey( associationKeyMetadata.getEntityKeyMetadata(), oldColumnValue ); if ( gridDialect instanceof BatchOperationsDelegator && ( (BatchOperationsDelegator) gridDialect ).isMarkedForRemoval( entityKey ) ) { return; } AssociationPersister associationPersister = createInverseAssociationPersister( propertyIndex, associationKeyMetadata, oldColumnValue ); Association association = associationPersister.getAssociationOrNull(); // The association might be empty if the navigation information have already been removed. // This typically happens when the entity owning the inverse association has already been deleted prior to // deleting the entity owning the association and a {@code @NotFound(action = NotFoundAction.IGNORE)} is // involved. if ( association != null && !association.isEmpty() ) { RowKey rowKey = getInverseRowKey( associationKeyMetadata, oldColumnValue ); association.remove( rowKey ); associationPersister.flushToDatastore(); } }
@Override public void insertRows(PersistentCollection collection, Serializable id, SharedSessionContractImplementor session) throws HibernateException { if ( !isInverse && isRowInsertEnabled() ) { if ( log.isDebugEnabled() ) { log.debug( "Inserting rows of collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) ); } AssociationPersister associationPersister = getAssociationPersister( collection.getOwner(), id, session ); // insert all the new entries collection.preInsert( this ); Iterator<?> entries = collection.entries( this ); int i = 0; int count = 0; while ( entries.hasNext() ) { Object entry = entries.next(); if ( collection.needsInserting( entry, i, elementType ) ) { // TODO: copy/paste from recreate() RowKeyAndTuple associationRow = createAndPutAssociationRowForInsert( id, collection, associationPersister, session, i, entry ); updateInverseSideOfAssociationNavigation( session, entry, associationPersister.getAssociationKey(), associationRow.tuple, Action.ADD, associationRow.key ); collection.afterRowInsert( this, entry, i ); count++; } i++; } associationPersister.flushToDatastore(); if ( log.isDebugEnabled() ) { log.debug( "done inserting rows: " + count + " inserted" ); } } }
associationPersister.getAssociation().put( inverseRowKey, inverseAssociationRow ); for ( String columnName : inverseRowKey.getColumnNames() ) { inverseAssociationRow.put( columnName, associationRow.get( columnName ) ); associationPersister.getAssociation().put( inverseRowKey, inverseAssociationRow ); associationPersister.getAssociation().remove( inverseRowKey ); associationPersister.flushToDatastore();
@Override public int getSize(Serializable key, SharedSessionContractImplementor session) { AssociationPersister associationPersister = getAssociationPersister( session.getPersistenceContext().getEntity( new org.hibernate.engine.spi.EntityKey( key, getOwnerEntityPersister() ) ), key, session ); final Association collectionMetadata = associationPersister.getAssociationOrNull(); return collectionMetadata == null ? 0 : collectionMetadata.size(); }
/** * Whether the association in question is stored within an entity structure ("embedded") and this entity has * properties whose value is generated in the datastore (such as a version attribute) or not. * * @return {@code true} in case the represented association is stored within an entity which has server-generated * properties, and thus must be re-read after an update to the association, {@code false} otherwise. */ public boolean hostingEntityRequiresReadAfterUpdate() { if ( hostingEntityRequiresReadAfterUpdate == null ) { boolean storedInEntityStructure = gridDialect.isStoredInEntityStructure( associationKeyMetadata, associationTypeContext ); boolean hasUpdateGeneratedProperties = getHostingEntityPersister().hasUpdateGeneratedProperties(); hostingEntityRequiresReadAfterUpdate = storedInEntityStructure && hasUpdateGeneratedProperties; } return hostingEntityRequiresReadAfterUpdate; }
/** * Creates an association row representing the given entry and adds it to the association managed by the given * persister. */ private RowKeyAndTuple createAndPutAssociationRowForInsert(Serializable key, PersistentCollection collection, AssociationPersister associationPersister, SharedSessionContractImplementor session, int i, Object entry) { RowKeyBuilder rowKeyBuilder = initializeRowKeyBuilder(); Tuple associationRow = new Tuple(); // the collection has a surrogate key (see @CollectionId) if ( hasIdentifier ) { final Object identifier = collection.getIdentifier( entry, i ); String[] names = { getIdentifierColumnName() }; identifierGridType.nullSafeSet( associationRow, identifier, names, session ); } getKeyGridType().nullSafeSet( associationRow, key, getKeyColumnNames(), session ); // No need to write to where as we don't do where clauses in OGM :) if ( hasIndex ) { Object index = collection.getIndex( entry, i, this ); indexGridType.nullSafeSet( associationRow, incrementIndexByBase( index ), getIndexColumnNames(), session ); } // columns of referenced key final Object element = collection.getElement( entry ); getElementGridType().nullSafeSet( associationRow, element, getElementColumnNames(), session ); RowKeyAndTuple result = new RowKeyAndTuple(); result.key = rowKeyBuilder.values( associationRow ).build(); result.tuple = associationRow; associationPersister.getAssociation().put( result.key, result.tuple ); return result; }
Tuple assocEntryTuple = associationPersister.getAssociation().get( assocEntryKey ); if ( assocEntryTuple == null ) { throw new AssertionFailure( "Deleting a collection tuple that is not present: " + "table {" + getTableName() + "} collectionKey {" + id + "} entry {" + entry + "}" ); updateInverseSideOfAssociationNavigation( session, entry, associationPersister.getAssociationKey(), assocEntryTuple, Action.REMOVE, assocEntryKey ); associationPersister.getAssociation().remove( assocEntryKey ); associationPersister.flushToDatastore();
public Association getAssociationOrNull() { if ( association == null ) { if ( hostingEntity != null ) { OgmEntityEntryState entryState = OgmEntityEntryState.getStateFor( session, hostingEntity ); if ( entryState.hasAssociation( associationKeyMetadata.getCollectionRole() ) ) { association = entryState.getAssociation( associationKeyMetadata.getCollectionRole() ); return association; } } if ( association == null ) { association = gridDialect.getAssociation( getAssociationKey(), getAssociationContext() ); if ( hostingEntity != null ) { OgmEntityEntryState.getStateFor( session, hostingEntity ) .setAssociation( associationKeyMetadata.getCollectionRole(), association ); } } } return association; }
private void removeNavigationInformation(Serializable id, Object entity, SharedSessionContractImplementor session) { for ( int propertyIndex = 0; propertyIndex < getEntityMetamodel().getPropertySpan(); propertyIndex++ ) { if ( propertyMightHaveNavigationalInformation[propertyIndex] ) { CollectionType collectionType = (CollectionType) getPropertyTypes()[propertyIndex]; OgmCollectionPersister collectionPersister = (OgmCollectionPersister) getFactory() .getMetamodel().collectionPersister( collectionType.getRole() ); AssociationPersister associationPersister = new AssociationPersister.Builder( collectionPersister.getOwnerEntityPersister().getMappedClass() ) .hostingEntity( entity ) .gridDialect( gridDialect ) .key( id, collectionPersister.getKeyGridType() ) .associationKeyMetadata( collectionPersister.getAssociationKeyMetadata() ) .associationTypeContext( collectionPersister.getAssociationTypeContext() ) .session( session ) .build(); Association association = associationPersister.getAssociationOrNull(); if ( association != null && !association.isEmpty() ) { association.clear(); associationPersister.flushToDatastore(); } } } }
updateInverseSideOfAssociationNavigation( session, entry, associationPersister.getAssociationKey(), keyAndTuple.tuple, Action.ADD, keyAndTuple.key ); collection.afterRowInsert( this, entry, i ); count++; associationPersister.flushToDatastore();
.build(); final Association ids = associationPersister.getAssociationOrNull();
.build(); Association assoc = associationPersister.getAssociationOrNull(); if ( assoc != null ) { for ( RowKey rowKey : assoc.getKeys() ) {