/** * Get the current state of the entity as known to the underlying * database, or null if there is no corresponding row * <p/> * {@inheritDoc} */ @Override public Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister) throws HibernateException { final EntityKey key = session.generateEntityKey( id, persister ); final Object cached = entitySnapshotsByKey.get( key ); if ( cached != null ) { return cached == NO_ROW ? null : (Object[]) cached; } else { final Object[] snapshot = persister.getDatabaseSnapshot( id, session ); entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot ); return snapshot; } }
private static EntityKey generateEntityKeyOrNull(Serializable id, SharedSessionContractImplementor s, String entityName) { if ( id == null || s == null || entityName == null ) { return null; } return s.generateEntityKey( id, s.getFactory().getEntityPersister( entityName ) ); }
@Override protected EntityKey getEntityKey() { return getSession().generateEntityKey( getId(), getPersister() ); }
/** * Remove the entity key with the specified {@code id} and {@code persister} from * the batch loadable entities {@link BatchFetchQueue}. * * @param id - the ID for the entity to be removed * @param persister - the entity persister * @param session - the session */ public static void removeBatchLoadableEntityKey( Serializable id, EntityPersister persister, SharedSessionContractImplementor session) { final EntityKey entityKey = session.generateEntityKey( id, persister ); final BatchFetchQueue batchFetchQueue = session.getPersistenceContext().getBatchFetchQueue(); batchFetchQueue.removeBatchLoadableEntityKey( entityKey ); } }
private EntityKey generateDelayedEntityKey() { if ( !isDelayed ) { throw new AssertionFailure( "cannot request delayed entity-key for early-insert post-insert-id generation" ); } return getSession().generateEntityKey( getDelayedId(), getPersister() ); } }
/** * Reconnect to session after deserialization... * * @param session The session being deserialized */ @Override public void afterDeserialize(SharedSessionContractImplementor session) { if ( this.session != null || this.persister != null ) { throw new IllegalStateException( "already attached to a session." ); } // IMPL NOTE: non-flushed changes code calls this method with session == null... // guard against NullPointerException if ( session != null ) { this.session = session; this.persister = session.getFactory().getMetamodel().entityPersister( entityName ); this.instance = session.getPersistenceContext().getEntity( session.generateEntityKey( id, persister ) ); } }
private CollectionInitializer getSubselectInitializer(Serializable key, SharedSessionContractImplementor session) { if ( !isSubselectLoadable() ) { return null; } final PersistenceContext persistenceContext = session.getPersistenceContext(); SubselectFetch subselect = persistenceContext.getBatchFetchQueue() .getSubselect( session.generateEntityKey( key, getOwnerEntityPersister() ) ); if ( subselect == null ) { return null; } else { // Take care of any entities that might have // been evicted! Iterator iter = subselect.getResult().iterator(); while ( iter.hasNext() ) { if ( !persistenceContext.containsEntity( (EntityKey) iter.next() ) ) { iter.remove(); } } // Run a subquery loader return createSubselectInitializer( subselect, session ); } }
private static EntityKey getOptionalObjectKey(QueryParameters queryParameters, SharedSessionContractImplementor session) { final Object optionalObject = queryParameters.getOptionalObject(); final Serializable optionalId = queryParameters.getOptionalId(); final String optionalEntityName = queryParameters.getOptionalEntityName(); if ( optionalObject != null && optionalEntityName != null ) { return session.generateEntityKey( optionalId, session.getEntityPersister( optionalEntityName, optionalObject ) ); } else { return null; } }
@Override public boolean isNull(Object owner, SharedSessionContractImplementor session) { if ( propertyName != null ) { final EntityPersister ownerPersister = session.getFactory().getMetamodel().entityPersister( entityName ); final Serializable id = session.getContextEntityIdentifier( owner ); final EntityKey entityKey = session.generateEntityKey( id, ownerPersister ); return session.getPersistenceContext().isPropertyNull( entityKey, getPropertyName() ); } else { return false; } }
/** * Associate a proxy that was instantiated by another session with this session * * @param li The proxy initializer. * @param proxy The proxy to reassociate. */ private void reassociateProxy(LazyInitializer li, HibernateProxy proxy) { if ( li.getSession() != this.getSession() ) { final EntityPersister persister = session.getFactory().getMetamodel().entityPersister( li.getEntityName() ); final EntityKey key = session.generateEntityKey( li.getIdentifier(), persister ); // any earlier proxy takes precedence proxiesByKey.putIfAbsent( key, proxy ); proxy.getHibernateLazyInitializer().setSession( session ); } }
@Override public void replaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, Serializable generatedId) { final Object entity = entitiesByKey.remove( oldKey ); final EntityEntry oldEntry = entityEntryContext.removeEntityEntry( entity ); parentsByChild.clear(); final EntityKey newKey = session.generateEntityKey( generatedId, oldEntry.getPersister() ); addEntity( newKey, entity ); addEntry( entity, oldEntry.getStatus(), oldEntry.getLoadedState(), oldEntry.getRowId(), generatedId, oldEntry.getVersion(), oldEntry.getLockMode(), oldEntry.isExistsInDatabase(), oldEntry.getPersister(), oldEntry.isBeingReplicated() ); }
/** * Register the entity as batch loadable, if enabled */ @SuppressWarnings({ "JavaDoc" }) private void scheduleBatchLoadIfNeeded(Serializable id, SharedSessionContractImplementor session) throws MappingException { //cannot batch fetch by unique key (property-ref associations) if ( uniqueKeyPropertyName == null && id != null ) { final EntityPersister persister = getAssociatedEntityPersister( session.getFactory() ); if ( persister.isBatchLoadable() ) { final EntityKey entityKey = session.generateEntityKey( id, persister ); if ( !session.getPersistenceContext().containsEntity( entityKey ) ) { session.getPersistenceContext().getBatchFetchQueue().addBatchLoadableEntityKey( entityKey ); } } } }
return resultId == null ? null : session.generateEntityKey( resultId, persister );
session ); return session.generateEntityKey( identifierState, entityPersister ); return session.generateEntityKey( optionalId, entityPersister );
/** * Attempt to initialize the proxy without loading anything from the database. * * This will only have any effect if the proxy is still attached to a session, * and the entity being proxied has been loaded and added to the persistence context * of that session since the proxy was created. */ public final void initializeWithoutLoadIfPossible() { if ( !initialized && session != null && session.isOpenOrWaitingForAutoClose() ) { final EntityKey key = session.generateEntityKey( getIdentifier(), session.getFactory().getMetamodel().entityPersister( getEntityName() ) ); final Object entity = session.getPersistenceContext().getEntity( key ); if ( entity != null ) { setImplementation( entity ); } } }
Status.READ_ONLY, loadedState, session.generateEntityKey( id, persister ), persister.getVersion( target ), LockMode.NONE,
@Override public Object readRow(ResultSet resultSet, ResultSetProcessingContextImpl context) throws SQLException { final ResultSetProcessingContext.EntityReferenceProcessingState processingState = rootReturnReader.getIdentifierResolutionContext( context ); // if the entity reference we are hydrating is a Return, it is possible that its EntityKey is // supplied by the QueryParameter optional entity information if ( context.shouldUseOptionalEntityInformation() && context.getQueryParameters().getOptionalId() != null ) { final EntityKey entityKey = context.getSession().generateEntityKey( context.getQueryParameters().getOptionalId(), processingState.getEntityReference().getEntityPersister() ); processingState.registerIdentifierHydratedForm( entityKey.getIdentifier() ); processingState.registerEntityKey( entityKey ); } return super.readRow( resultSet, context ); }
if ( resolvedId != null ) { processingState.registerEntityKey( context.getSession().generateEntityKey( resolvedId, entityReference.getEntityPersister() ) );
@Override public Object load(Serializable id, Object optionalObject, SharedSessionContractImplementor session) { LOG.debugf( "Loading entity: %s using named query: %s", persister.getEntityName(), queryName ); // IMPL NOTE: essentially we perform the named query (which loads the entity into the PC), and then // do an internal lookup of the entity from the PC. final AbstractProducedQuery query = (AbstractProducedQuery) session.getNamedQuery( queryName ); if ( query.getParameterMetadata().hasNamedParameters() ) { query.setParameter( query.getNamedParameters()[0], id, persister.getIdentifierType() ); } else { query.setParameter( position, id, persister.getIdentifierType() ); } query.setOptionalId( id ); query.setOptionalEntityName( persister.getEntityName() ); query.setOptionalObject( optionalObject ); query.setFlushMode( FlushMode.MANUAL ); query.list(); // now look up the object we are really interested in! // (this lets us correctly handle proxies and multi-row or multi-column queries) return session.getPersistenceContext().getEntity( session.generateEntityKey( id, persister ) ); } }
entityKey = session.generateEntityKey( generatedId, persister ); session.getPersistenceContext().checkUniqueness( entityKey, getInstance() );