Package org.hibernate.ogm.util.impl

Examples of org.hibernate.ogm.util.impl.AssociationPersister


      if ( log.isDebugEnabled() ) {
        log.debug( "Inserting collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) );
      }

      AssociationPersister associationPersister = new AssociationPersister(
          getOwnerEntityPersister().getMappedClass()
        )
        .hostingEntity( collection.getOwner() )
        .gridDialect( gridDialect )
        .key( id )
        .keyGridType( getKeyGridType() )
        .associationKeyMetadata( associationKeyMetadata )
        .collectionPersister( this )
        .session( session );

      // create all the new entries
      Iterator<?> entries = collection.entries( this );
      if ( entries.hasNext() ) {
        collection.preInsert( this );
        int i = 0;
        int count = 0;
        while ( entries.hasNext() ) {
          final Object entry = entries.next();
          if ( collection.entryExists( entry, i ) ) {
            // TODO: copy/paste from insertRows()
            RowKeyAndTuple keyAndTuple = createAndPutTupleforInsert( id, collection, associationPersister, session, i, entry );
            completeTuple( keyAndTuple, collection, session, entry );
            updateInverseSideOfAssociationNavigation( session, entry, keyAndTuple.tuple, Action.ADD, keyAndTuple.key );
            collection.afterRowInsert( this, entry, i );
            count++;
          }
          i++;
        }

        associationPersister.flushToCache();

        if ( log.isDebugEnabled() ) {
          log.debug( "done inserting collection: " + count + " rows inserted" );
        }
View Full Code Here


    else if ( associationType == AssociationType.ASSOCIATION_TABLE_TO_ENTITY ) {
      String[] elementColumnNames = getElementColumnNames();
      Object[] elementColumnValues = LogicalPhysicalConverterHelper.getColumnValuesFromResultset( tuple, elementColumnNames );
      Serializable entityId = (Serializable) gridTypeOfAssociatedId.nullSafeGet( tuple, getElementColumnNames(), session, null );

      AssociationPersister associationPersister = new AssociationPersister(
            getElementPersister().getMappedClass()
          )
          .gridDialect( gridDialect )
          .keyColumnValues( elementColumnValues )
          .session( session )
          .associationKeyMetadata( associationKeyMetadataFromElement )
          .collectionPersister( this )
          .key( entityId )
          .inverse();

      // TODO what happens when a row should be *updated* ?: I suspect ADD works OK as it's a put()
      if ( action == Action.ADD ) {
        // FIXME build the key
        Tuple assocTuple = associationPersister.createAndPutAssociationTuple( rowKey );
        for ( String columnName : tuple.getColumnNames() ) {
          assocTuple.put( columnName, tuple.get( columnName ) );
        }
        associationPersister.getAssociation().put( rowKey, assocTuple );
      }
      else if ( action == Action.REMOVE ) {
        // we try and match the whole tuple as it should be on both sides of the navigation
        if ( rowKey == null ) {
          throw new AssertionFailure( "Deleting a collection tuple that is not present: " + "table {"
              + getTableName() + "} key column names {" + Arrays.toString( elementColumnNames )
              + "} key column values {" + Arrays.toString( elementColumnValues ) + "}" );
        }
        associationPersister.getAssociation().remove( rowKey );
      }
      else {
        throw new AssertionFailure( "Unknown action type: " + action );
      }

      if ( associationPersister.hostingEntityRequiresReadAfterUpdate() && entity == null ) {
        entity = session.getPersistenceContext().getEntity( session.generateEntityKey( entityId, getElementPersister() ) );
      }

      associationPersister.hostingEntity( entity );

      associationPersister.flushToCache();
    }
  }
View Full Code Here

      if ( log.isDebugEnabled() ) {
        log.debug( "Deleting collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) );
      }

      // Remove all the old entries
      AssociationPersister associationPersister = new AssociationPersister(
            getOwnerEntityPersister().getMappedClass()
          )
          .gridDialect( gridDialect )
          .key( id )
          .keyGridType( getKeyGridType() )
          .associationKeyMetadata( associationKeyMetadata )
          .collectionPersister( this )
          .session( session );

      Association association = associationPersister.getAssociationOrNull();

      if ( association != null ) {
        // shortcut to avoid loop if we can
        if ( associationType != AssociationType.OTHER ) {
          for ( RowKey assocEntryKey : association.getKeys() ) {
            // we unfortunately cannot mass change the update of the associated entity
            updateInverseSideOfAssociationNavigation(
                session,
                null,
                association.get( assocEntryKey ),
                Action.REMOVE,
                assocEntryKey
                );
          }
        }
        association.clear();

        if ( associationPersister.hostingEntityRequiresReadAfterUpdate() ) {
          Object owner = session.getPersistenceContext().getCollectionOwner( id, this );
          associationPersister.hostingEntity( owner );
        }

        associationPersister.flushToCache();
      }

      if ( log.isDebugEnabled() ) {
        log.debug( "done deleting collection" );
      }
View Full Code Here

    String[] rowKeyColumnNames = buildRowKeyColumnNamesForStarToOne( persister, propertyColumnNames );

    AssociationKeyMetadata associationKeyMetadata = new AssociationKeyMetadata( persister.getTableName( tableIndex ), propertyColumnNames );
    associationKeyMetadata.setRowKeyColumnNames( rowKeyColumnNames );

    AssociationPersister associationPersister = new AssociationPersister(
          persister.getPropertyTypes()[propertyIndex].getReturnedClass()
        )
        .hostingEntity( getReferencedEntity( propertyIndex ) )
        .gridDialect( gridDialect )
        .associationKeyMetadataassociationKeyMetadata )
        .keyColumnValues( newColumnValue )
        .session( session )
        //does not set .collectionPersister as it does not make sense here for a ToOne or a unique key
        .propertyType( persister.getPropertyTypes()[propertyIndex] );
    Tuple tuple = new Tuple();
    //add the id column
    final String[] identifierColumnNames = persister.getIdentifierColumnNames();
    gridIdentifierType.nullSafeSet( tuple, id, identifierColumnNames, session );
    //add the fk column
    gridPropertyTypes[propertyIndex].nullSafeSet(
              tuple,
              fields[propertyIndex],
              propertyColumnNames,
              includeColumns[propertyIndex],
              session
          );
    Object[] columnValues = LogicalPhysicalConverterHelper.getColumnValuesFromResultset( tuple, rowKeyColumnNames );
    final RowKey rowKey = new RowKey( persister.getTableName(), rowKeyColumnNames, columnValues );

    Tuple assocEntryTuple = associationPersister.createAndPutAssociationTuple( rowKey );
    for ( String column : tuple.getColumnNames() ) {
      assocEntryTuple.put( column, tuple.get( column ) );
    }

    associationPersister.flushToCache();
  }
View Full Code Here

    String[] rowKeyColumnNames = buildRowKeyColumnNamesForStarToOne( persister, propertyColumnNames );

    AssociationKeyMetadata associationKeyMetadata = new AssociationKeyMetadata( persister.getTableName( tableIndex ), propertyColumnNames );
    associationKeyMetadata.setRowKeyColumnNames( rowKeyColumnNames );

    AssociationPersister associationPersister = new AssociationPersister(
          persister.getPropertyTypes()[propertyIndex].getReturnedClass()
        )
        .hostingEntity( getReferencedEntity( propertyIndex ) )
        .gridDialect( gridDialect )
        .associationKeyMetadata( associationKeyMetadata )
        .keyColumnValues( oldColumnValue )
        .session( session )
        //does not set .collectionPersister as it does not make sense here for a ToOne or a unique key
        .propertyType( persister.getPropertyTypes()[propertyIndex] );
    //add fk column value in TupleKey
    Tuple tupleKey = new Tuple();
    for (int index = 0 ; index < propertyColumnNames.length ; index++) {
      tupleKey.put( propertyColumnNames[index], oldColumnValue[index] );
    }
    //add id value in TupleKey
    gridIdentifierType.nullSafeSet( tupleKey, id, persister.getIdentifierColumnNames(), session );

    Association propertyValues = associationPersister.getAssociation();
    if ( propertyValues != null ) {
      //Map's equals operation delegates to all it's key and value, should be fine for now
      //this is a StarToOne case ie the FK is on the owning entity
      final RowKey matchingTuple = new RowKeyBuilder()
          .tableName( persister.getTableName() )
          .addColumns( buildRowKeyColumnNamesForStarToOne( persister, propertyColumnNames ) )
          .values( tupleKey )
          .build();
      //TODO what should we do if that's null?
      associationPersister.getAssociation().remove( matchingTuple );

      associationPersister.flushToCache();
    }
  }
View Full Code Here

      return 0;
    }
    int count = 0;
    int i = 0;
    Iterator<?> entries = collection.entries( this );
    AssociationPersister associationPersister = new AssociationPersister(
          getOwnerEntityPersister().getMappedClass()
        )
        .hostingEntity( collection.getOwner() )
        .gridDialect( gridDialect )
        .key( key )
        .keyGridType( getKeyGridType() )
        .associationKeyMetadata( associationKeyMetadata )
        .collectionPersister( this )
        .session( session );

    while ( entries.hasNext() ) {
      Object entry = entries.next();
      if ( collection.needsUpdating( entry, i, elementType ) ) {
        // find the matching element
        RowKey assocEntryKey = getTupleKeyForUpdate( key, collection, session, i, entry );
        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 + "}" );
        }
        // update the matching element
        // FIXME update the associated entity key data
        updateInverseSideOfAssociationNavigation( session, entry, assocEntryTuple, Action.REMOVE, assocEntryKey );

        getElementGridType().nullSafeSet(
            assocEntryTuple,
            collection.getElement( entry ),
            getElementColumnNames(),
            session
        );

        // put back entry tuple to actually apply changes to the store
        associationPersister.getAssociation().put( assocEntryKey, assocEntryTuple );

        updateInverseSideOfAssociationNavigation( session, entry, assocEntryTuple, Action.ADD, assocEntryKey );

        count++;
      }
      i++;
    }

    // need to put the data back in the cache
    associationPersister.flushToCache();

    return count;
  }
View Full Code Here

    return rowKeyBuilder.build();
  }

  @Override
  public int getSize(Serializable key, SessionImplementor session) {
    AssociationPersister associationPersister = new AssociationPersister(
          getOwnerEntityPersister().getMappedClass()
        )
        .key( key )
        .session( session )
        .gridDialect( gridDialect )
        .keyGridType( getKeyGridType() )
        .associationKeyMetadata( associationKeyMetadata )
        .collectionPersister( this );

    final Association collectionMetadata = associationPersister.getAssociationOrNull();
    return collectionMetadata == null ? 0 : collectionMetadata.size();
  }
View Full Code Here

        log.debug( "Deleting rows of collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) );
      }

      boolean deleteByIndex = !isOneToMany() && hasIndex && !indexContainsFormula;

      AssociationPersister associationPersister = new AssociationPersister(
          getOwnerEntityPersister().getMappedClass()
        )
        .hostingEntity( collection.getOwner() )
        .gridDialect( gridDialect )
        .key( id )
        .keyGridType( getKeyGridType() )
        .associationKeyMetadata( associationKeyMetadata )
        .collectionPersister( this )
        .session( session );

      // delete all the deleted entries
      Iterator<?> deletes = collection.getDeletes( this, !deleteByIndex );
      if ( deletes.hasNext() ) {
        int count = 0;
        while ( deletes.hasNext() ) {
          Object entry = deletes.next();
          // find the matching element
          RowKey assocEntryKey = getTupleKeyForDelete( id, collection, session, entry, deleteByIndex );
          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 + "}" );
          }
          // delete the tuple
          updateInverseSideOfAssociationNavigation( session, entry, assocEntryTuple, Action.REMOVE, assocEntryKey );
          associationPersister.getAssociation().remove( assocEntryKey );

          count++;

          if ( log.isDebugEnabled() ) {
            log.debug( "done deleting collection rows: " + count + " deleted" );
          }
        }

        associationPersister.flushToCache();
      }
      else {
        log.debug( "no rows to delete" );
      }
    }
View Full Code Here

      if ( log.isDebugEnabled() ) {
        log.debug( "Inserting rows of collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) );
      }

      AssociationPersister associationPersister = new AssociationPersister(
          getOwnerEntityPersister().getMappedClass()
        )
        .hostingEntity( collection.getOwner() )
        .gridDialect( gridDialect )
        .key( id )
        .keyGridType( getKeyGridType() )
        .associationKeyMetadata( associationKeyMetadata )
        .collectionPersister( this )
        .session( 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 keyAndTuple = createAndPutTupleforInsert( id, collection, associationPersister, session, i, entry );
          completeTuple( keyAndTuple, collection, session, entry );
          updateInverseSideOfAssociationNavigation( session, entry, keyAndTuple.tuple, Action.ADD, keyAndTuple.key );
          collection.afterRowInsert( this, entry, i );
          count++;
        }
        i++;
      }

      associationPersister.flushToCache();

      if ( log.isDebugEnabled() ) {
        log.debug( "done inserting rows: " + count + " inserted" );
      }
    }
View Full Code Here

      if ( log.isDebugEnabled() ) {
        log.debug( "Inserting collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) );
      }

      AssociationPersister associationPersister = new AssociationPersister(
          getOwnerEntityPersister().getMappedClass()
        )
        .hostingEntity( collection.getOwner() )
        .gridDialect( gridDialect )
        .key( id )
        .keyGridType( getKeyGridType() )
        .associationKeyMetadata( associationKeyMetadata )
        .collectionPersister( this )
        .session( session );

      // create all the new entries
      Iterator<?> entries = collection.entries( this );
      if ( entries.hasNext() ) {
        collection.preInsert( this );
        int i = 0;
        int count = 0;
        while ( entries.hasNext() ) {
          final Object entry = entries.next();
          if ( collection.entryExists( entry, i ) ) {
            // TODO: copy/paste from insertRows()
            RowKeyAndTuple keyAndTuple = createAndPutTupleforInsert( id, collection, associationPersister, session, i, entry );
            completeTuple( keyAndTuple, collection, session, entry );
            updateInverseSideOfAssociationNavigation( session, entry, keyAndTuple.tuple, Action.ADD, keyAndTuple.key );
            collection.afterRowInsert( this, entry, i );
            count++;
          }
          i++;
        }

        associationPersister.flushToCache();

        if ( log.isDebugEnabled() ) {
          log.debug( "done inserting collection: " + count + " rows inserted" );
        }
View Full Code Here

TOP

Related Classes of org.hibernate.ogm.util.impl.AssociationPersister

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.