Package org.hibernate.ogm.util.impl

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


    //find the ids per unique property name
    AssociationKeyMetadata associationKeyMetadata = associationKeyMetadataPerPropertyName.get( propertyName );
    if ( associationKeyMetadata == null ) {
      throw new AssertionFailure( "loadByUniqueKey on a non EntityType:" + propertyName );
    }
    AssociationPersister associationPersister = new AssociationPersister(
          getMappedClass()
        )
        .gridDialect( gridDialect )
        .key( uniqueKey )
        .keyGridType( gridUniqueKeyType )
        //does not set .collectionPersister as it does not make sense here for an entity
        .associationKeyMetadata( associationKeyMetadata )
        .session( session )
        .propertyType( getPropertyTypes()[propertyIndex] );
    final Association ids = associationPersister.getAssociationOrNull();

    if (ids == null || ids.size() == 0 ) {
      return null;
    }
    else if (ids.size() == 1) {
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
        );

        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

    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 );

      // shortcut to avoid loop if we can
      if ( associationType != AssociationType.OTHER ) {
        for ( RowKey assocEntryKey : associationPersister.getAssociation().getKeys() ) {
          // we unfortunately cannot mass change the update of the associated entity
          updateInverseSideOfAssociationNavigation(
              session,
              null,
              associationPersister.getAssociation().get( assocEntryKey ),
              Action.REMOVE,
              assocEntryKey
          );
        }
      }
      associationPersister.getAssociation().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

      //collection persister
      if ( getCollectionPersisters().length != 1 ) {
        throw new AssertionFailure( "Found an unexpected number of collection persisters: " + getCollectionPersisters().length );
      }
      final OgmCollectionPersister persister = (OgmCollectionPersister) getCollectionPersisters()[0];
      AssociationPersister associationPersister = new AssociationPersister(
          persister.getOwnerEntityPersister().getMappedClass()
        )
        .gridDialect( gridDialect )
        .key( id )
        .keyGridType( persister.getKeyGridType() )
        .collectionPersister( persister )
        .associationKeyMetadata( persister.getAssociationKeyMetadata() )
        .session( session );
      Association assoc = associationPersister.getAssociationOrNull();
      if ( assoc != null ) {
        for ( RowKey rowKey : assoc.getKeys() ) {
          resultset.addTuple( assoc.get( rowKey ) );
        }
      }
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

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.