Java Code Examples for org.hibernate.engine.spi.PersistenceContext#getEntity()
The following examples show how to use
org.hibernate.engine.spi.PersistenceContext#getEntity() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultReactiveLoadEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 5 votes |
/** * If there is already a corresponding proxy associated with the * persistence context, return it; otherwise create a proxy, associate it * with the persistence context, and return the just-created proxy. * * @param event The initiating load request event * @param persister The persister corresponding to the entity to be loaded * @param keyToLoad The key of the entity to be loaded * @param options The defined load options * @param persistenceContext The originating session * * @return The created/existing proxy */ private Object createProxyIfNecessary( final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options, final PersistenceContext persistenceContext) { Object existing = persistenceContext.getEntity( keyToLoad ); final boolean traceEnabled = LOG.isTraceEnabled(); if ( existing != null ) { // return existing object or initialized proxy (unless deleted) if ( traceEnabled ) { LOG.trace( "Entity found in session cache" ); } if ( options.isCheckDeleted() ) { EntityEntry entry = persistenceContext.getEntry( existing ); Status status = entry.getStatus(); if ( status == Status.DELETED || status == Status.GONE ) { return null; } } return existing; } if ( traceEnabled ) { LOG.trace( "Creating new proxy for entity" ); } return createProxy( event, persister, keyToLoad, persistenceContext ); }
Example 2
Source File: AbstractEntityTuplizer.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void setIdentifier(Object entity, Serializable id, EntityMode entityMode, SharedSessionContractImplementor session) { final Object[] extractedValues = mappedIdentifierType.getPropertyValues( id, entityMode ); final Object[] injectionValues = new Object[extractedValues.length]; final PersistenceContext persistenceContext = session.getPersistenceContext(); for ( int i = 0; i < virtualIdComponent.getSubtypes().length; i++ ) { final Type virtualPropertyType = virtualIdComponent.getSubtypes()[i]; final Type idClassPropertyType = mappedIdentifierType.getSubtypes()[i]; if ( virtualPropertyType.isEntityType() && !idClassPropertyType.isEntityType() ) { if ( session == null ) { throw new AssertionError( "Deprecated version of getIdentifier (no session) was used but session was required" ); } final String associatedEntityName = ( (EntityType) virtualPropertyType ).getAssociatedEntityName(); final EntityKey entityKey = session.generateEntityKey( (Serializable) extractedValues[i], sessionFactory.getMetamodel().entityPersister( associatedEntityName ) ); // it is conceivable there is a proxy, so check that first Object association = persistenceContext.getProxy( entityKey ); if ( association == null ) { // otherwise look for an initialized version association = persistenceContext.getEntity( entityKey ); if ( association == null ) { // get the association out of the entity itself association = sessionFactory.getMetamodel().entityPersister( entityName ).getPropertyValue( entity, virtualIdComponent.getPropertyNames()[i] ); } } injectionValues[i] = association; } else { injectionValues[i] = extractedValues[i]; } } virtualIdComponent.setPropertyValues( entity, injectionValues, entityMode ); }
Example 3
Source File: DefaultLoadEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
/** * If there is already a corresponding proxy associated with the * persistence context, return it; otherwise create a proxy, associate it * with the persistence context, and return the just-created proxy. * * @param event The initiating load request event * @param persister The persister corresponding to the entity to be loaded * @param keyToLoad The key of the entity to be loaded * @param options The defined load options * @param persistenceContext The originating session * * @return The created/existing proxy */ private Object createProxyIfNecessary( final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options, final PersistenceContext persistenceContext) { Object existing = persistenceContext.getEntity( keyToLoad ); if ( existing != null ) { // return existing object or initialized proxy (unless deleted) if ( traceEnabled ) { LOG.trace( "Entity found in session cache" ); } if ( options.isCheckDeleted() ) { EntityEntry entry = persistenceContext.getEntry( existing ); Status status = entry.getStatus(); if ( status == Status.DELETED || status == Status.GONE ) { return null; } } return existing; } if ( traceEnabled ) { LOG.trace( "Creating new proxy for entity" ); } // return new uninitialized proxy Object proxy = persister.createProxy( event.getEntityId(), event.getSession() ); persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey( keyToLoad ); persistenceContext.addProxy( keyToLoad, proxy ); return proxy; }
Example 4
Source File: EntityType.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Load an instance by a unique key that is not the primary key. * * @param entityName The name of the entity to load * @param uniqueKeyPropertyName The name of the property defining the uniqie key. * @param key The unique key property value. * @param session The originating session. * * @return The loaded entity * * @throws HibernateException generally indicates problems performing the load. */ public Object loadByUniqueKey( String entityName, String uniqueKeyPropertyName, Object key, SharedSessionContractImplementor session) throws HibernateException { final SessionFactoryImplementor factory = session.getFactory(); UniqueKeyLoadable persister = (UniqueKeyLoadable) factory.getMetamodel().entityPersister( entityName ); //TODO: implement 2nd level caching?! natural id caching ?! proxies?! EntityUniqueKey euk = new EntityUniqueKey( entityName, uniqueKeyPropertyName, key, getIdentifierOrUniqueKeyType( factory ), persister.getEntityMode(), session.getFactory() ); final PersistenceContext persistenceContext = session.getPersistenceContext(); Object result = persistenceContext.getEntity( euk ); if ( result == null ) { result = persister.loadByUniqueKey( uniqueKeyPropertyName, key, session ); // If the entity was not in the Persistence Context, but was found now, // add it to the Persistence Context if (result != null) { persistenceContext.addEntity(euk, result); } } return result == null ? null : persistenceContext.proxyFor( result ); }
Example 5
Source File: ReactiveAbstractEntityPersister.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
default CompletionStage<?> deleteReactive( Serializable id, Object version, Object object, SharedSessionContractImplementor session) throws HibernateException { final int span = delegate().getTableSpan(); boolean isImpliedOptimisticLocking = !delegate().getEntityMetamodel().isVersioned() && isAllOrDirtyOptimisticLocking(); Object[] loadedState = null; if ( isImpliedOptimisticLocking ) { // need to treat this as if it where optimistic-lock="all" (dirty does *not* make sense); // first we need to locate the "loaded" state // // Note, it potentially could be a proxy, so doAfterTransactionCompletion the location the safe way... final EntityKey key = session.generateEntityKey( id, delegate()); final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); Object entity = persistenceContext.getEntity( key ); if ( entity != null ) { EntityEntry entry = persistenceContext.getEntry( entity ); loadedState = entry.getLoadedState(); } } final String[] deleteStrings; if ( isImpliedOptimisticLocking && loadedState != null ) { // we need to utilize dynamic delete statements deleteStrings = generateSQLDeleteStrings( loadedState ); } else { // otherwise, utilize the static delete statements deleteStrings = delegate().getSQLDeleteStrings(); } CompletionStage<?> deleteStage = CompletionStages.nullFuture(); for ( int j = span - 1; j >= 0; j-- ) { // For now we assume there is only one delete query int jj = j; Object[] state = loadedState; deleteStage = deleteStage.thenCompose( v-> deleteReactive( id, version, jj, object, deleteStrings[jj], session, state )); } return deleteStage; }
Example 6
Source File: AbstractReactiveSaveEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
/** * Prepares the save call by checking the session caches for a pre-existing * entity and performing any lifecycle callbacks. * * @param entity The entity to be saved. * @param id The id by which to save the entity. * @param persister The entity's persister instance. * @param useIdentityColumn Is an identity column being used? * @param context Generally cascade-specific information. * @param source The session from which the event originated. * @param requiresImmediateIdAccess does the event context require * access to the identifier immediately after execution of this method (if * not, post-insert style id generators may be postponed if we are outside * a transaction). * * @return The id used to save the entity; may be null depending on the * type of id generator used and the requiresImmediateIdAccess value */ protected CompletionStage<Void> reactivePerformSave( Object entity, Serializable id, EntityPersister persister, boolean useIdentityColumn, C context, EventSource source, boolean requiresImmediateIdAccess) { if ( LOG.isTraceEnabled() ) { LOG.tracev( "Saving {0}", MessageHelper.infoString( persister, id, source.getFactory() ) ); } final EntityKey key; if ( !useIdentityColumn ) { key = source.generateEntityKey( id, persister ); final PersistenceContext persistenceContext = source.getPersistenceContextInternal(); Object old = persistenceContext.getEntity( key ); if ( old != null ) { if ( persistenceContext.getEntry( old ).getStatus() == Status.DELETED ) { source.forceFlush( persistenceContext.getEntry( old ) ); } else { return CompletionStages.failedFuture( new NonUniqueObjectException( id, persister.getEntityName() ) ); } } persister.setIdentifier( entity, id, source ); } else { key = null; } return reactivePerformSaveOrReplicate( entity, key, persister, useIdentityColumn, context, source, requiresImmediateIdAccess ); }
Example 7
Source File: Collections.java From lams with GNU General Public License v2.0 | 4 votes |
private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) { final PersistenceContext persistenceContext = session.getPersistenceContext(); final CollectionEntry entry = persistenceContext.getCollectionEntry( coll ); final CollectionPersister loadedPersister = entry.getLoadedPersister(); if ( loadedPersister != null && LOG.isDebugEnabled() ) { LOG.debugf( "Collection dereferenced: %s", MessageHelper.collectionInfoString( loadedPersister, coll, entry.getLoadedKey(), session ) ); } // do a check final boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete(); if ( hasOrphanDelete ) { Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier( coll.getOwner(), session ); if ( ownerId == null ) { // the owning entity may have been deleted and its identifier unset due to // identifier-rollback; in which case, try to look up its identifier from // the persistence context if ( session.getFactory().getSessionFactoryOptions().isIdentifierRollbackEnabled() ) { final EntityEntry ownerEntry = persistenceContext.getEntry( coll.getOwner() ); if ( ownerEntry != null ) { ownerId = ownerEntry.getId(); } } if ( ownerId == null ) { throw new AssertionFailure( "Unable to determine collection owner identifier for orphan-delete processing" ); } } final EntityKey key = session.generateEntityKey( ownerId, loadedPersister.getOwnerEntityPersister() ); final Object owner = persistenceContext.getEntity( key ); if ( owner == null ) { throw new AssertionFailure( "collection owner not associated with session: " + loadedPersister.getRole() ); } final EntityEntry e = persistenceContext.getEntry( owner ); //only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete if ( e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE ) { throw new HibernateException( "A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " + loadedPersister.getRole() ); } } // do the work entry.setCurrentPersister( null ); entry.setCurrentKey( null ); prepareCollectionForUpdate( coll, entry, session.getFactory() ); }