@Override public int compareTo(Object other) { final CollectionAction action = (CollectionAction) other; // sort first by role name final int roleComparison = collectionRole.compareTo( action.collectionRole ); if ( roleComparison != 0 ) { return roleComparison; } else { //then by fk return persister.getKeyType().compare( key, action.key ); } }
@Override protected boolean isSame(CollectionPersister collectionPersister, Object oldObject, Object newObject) { final Map.Entry oldEntry = Map.Entry.class.cast( oldObject ); final Map.Entry newEntry = Map.Entry.class.cast( newObject ); if ( collectionPersister.getKeyType().isSame( oldEntry.getKey(), newEntry.getKey() ) ) { if ( collectionPersister.getElementType().isSame( oldEntry.getValue(), newEntry.getValue() ) ) { return true; } } return false; }
public static Object staticCreateCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { return new CacheKeyImplementation( id, persister.getKeyType(), persister.getRole(), tenantIdentifier, factory ); }
private CollectionKey( String role, Serializable key, Type keyType, EntityMode entityMode, SessionFactoryImplementor factory) { this.role = role; this.key = key; this.keyType = keyType; this.entityMode = entityMode; this.factory = factory; //cache the hash-code this.hashCode = generateHashCode(); }
@Override public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException { //we must use the "remembered" uk value, since it is //not available from the EntityEntry during assembly if (cached==null) { return null; } else { final Serializable key = (Serializable) getPersister(session) .getKeyType() .assemble( cached, session, owner); return resolveKey( key, session, owner, null ); } }
@Override public Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException { //remember the uk value //This solution would allow us to eliminate the owner arg to disassemble(), but //what if the collection was null, and then later had elements added? seems unsafe //session.getPersistenceContext().getCollectionEntry( (PersistentCollection) value ).getKey(); final Serializable key = getKeyOfOwner(owner, session); if (key==null) { return null; } else { return getPersister(session) .getKeyType() .disassemble( key, session, owner ); } }
@Override public void initialize(Serializable id, SharedSessionContractImplementor session) throws HibernateException { Serializable[] batch = session.getPersistenceContext().getBatchFetchQueue() .getCollectionBatch( getCollectionPersister(), id, batchSizes[0] ); for ( int i=0; i<batchSizes.length-1; i++) { final int smallBatchSize = batchSizes[i]; if ( batch[smallBatchSize-1]!=null ) { Serializable[] smallBatch = new Serializable[smallBatchSize]; System.arraycopy(batch, 0, smallBatch, 0, smallBatchSize); loaders[i].loadCollectionBatch( session, smallBatch, getCollectionPersister().getKeyType() ); return; //EARLY EXIT! } } loaders[batchSizes.length-1].loadCollection( session, id, getCollectionPersister().getKeyType() ); } }
public CollectionKey(CollectionPersister persister, Serializable key) { this( persister.getRole(), key, persister.getKeyType(), persister.getOwnerEntityPersister().getEntityMetamodel().getEntityMode(), persister.getFactory() ); }
public void initialize(Serializable key, SharedSessionContractImplementor session) throws HibernateException { LOG.debugf( "Initializing collection: %s using named query: %s", persister.getRole(), queryName ); NativeQueryImplementor nativeQuery = session.getNamedNativeQuery( queryName ); if ( nativeQuery.getParameterMetadata().hasNamedParameters() ) { nativeQuery.setParameter( nativeQuery.getParameterMetadata().getNamedParameterNames().iterator().next(), key, persister.getKeyType() ); } else { nativeQuery.setParameter( 1, key, persister.getKeyType() ); } nativeQuery.setCollectionKey( key ).setFlushMode( FlushMode.MANUAL ).list(); } }
@Override public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { if ( value == null ) { return "null"; } if ( !getReturnedClass().isInstance( value ) && !PersistentCollection.class.isInstance( value ) ) { // its most likely the collection-key final CollectionPersister persister = getPersister( factory ); if ( persister.getKeyType().getReturnedClass().isInstance( value ) ) { return getRole() + "#" + getPersister( factory ).getKeyType().toLoggableString( value, factory ); } else { // although it could also be the collection-id if ( persister.getIdentifierType() != null && persister.getIdentifierType().getReturnedClass().isInstance( value ) ) { return getRole() + "#" + getPersister( factory ).getIdentifierType().toLoggableString( value, factory ); } } } return renderLoggableString( value, factory ); }
/** * Compile a filter. This method may be called multiple * times. Subsequent invocations are no-ops. */ public synchronized void compile( String collectionRole, Map replacements, boolean scalar) throws QueryException, MappingException { if ( !isCompiled() ) { addFromAssociation( "this", collectionRole ); paramValueBinders.add( new CollectionFilterKeyParameterSpecification( collectionRole, getFactory().getMetamodel().collectionPersister( collectionRole ).getKeyType() ) ); compile( replacements, scalar ); } }
/** * Get the id value from the owning entity key, usually the same as the key, but might be some * other property, in the case of property-ref * * @param key The collection owner key * @param session The session from which the request is originating. * @return The collection owner's id, if it can be obtained from the key; * otherwise, null is returned */ public Serializable getIdOfOwnerOrNull(Serializable key, SharedSessionContractImplementor session) { Serializable ownerId = null; if ( foreignKeyPropertyName == null ) { ownerId = key; } else { Type keyType = getPersister( session ).getKeyType(); EntityPersister ownerPersister = getPersister( session ).getOwnerEntityPersister(); // TODO: Fix this so it will work for non-POJO entity mode Class ownerMappedClass = ownerPersister.getMappedClass(); if ( ownerMappedClass.isAssignableFrom( keyType.getReturnedClass() ) && keyType.getReturnedClass().isInstance( key ) ) { // the key is the owning entity itself, so get the ID from the key ownerId = ownerPersister.getIdentifier( key, session ); } else { // TODO: check if key contains the owner ID } } return ownerId; }
final boolean isEqual = collectionPersister.getKeyType().isEqual( id, ce.getLoadedKey(),
Type keyType = getPersister( session ).getKeyType(); Class returnedClass = keyType.getReturnedClass();
collectionType.getLHSPropertyName(), key, collectionPersister.getKeyType(), ownerPersister.getEntityMode(), session.getFactory()
CollectionFilterKeyParameterSpecification.PARAM_KEY, new TypedValue( entry.getLoadedPersister().getKeyType(), entry.getLoadedKey()
&& !currentPersister.getKeyType().isEqual( entry.getLoadedKey(), entry.getCurrentKey(), factory ) && !( entry.getLoadedKey() instanceof DelayedPostInsertIdentifier );
@Override public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { final CacheKey ck = session.generateCacheKey( key, persister.getKeyType(), persister.getRole() ); persister.getCacheAccessStrategy().unlockItem( ck, lock ); } }
protected final void evict() throws CacheException { if ( persister.hasCache() ) { CacheKey ck = session.generateCacheKey( key, persister.getKeyType(), persister.getRole() ); persister.getCacheAccessStrategy().remove( ck ); } }
public void afterTransactionCompletion(boolean success) throws CacheException { if ( persister.hasCache() ) { final CacheKey ck = new CacheKey( key, persister.getKeyType(), persister.getRole(), session.getEntityMode(), session.getFactory() ); persister.getCache().release(ck, lock); } }