Java Code Examples for org.hibernate.event.spi.PersistEvent#getSession()
The following examples show how to use
org.hibernate.event.spi.PersistEvent#getSession() .
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: DefaultReactivePersistEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 6 votes |
private CompletionStage<Void> entityIsDeleted(PersistEvent event, IdentitySet createCache) { final EventSource source = event.getSession(); final Object entity = source.getPersistenceContextInternal().unproxy( event.getObject() ); final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity ); if ( LOG.isTraceEnabled() ) { LOG.tracef( "un-scheduling entity deletion [%s]", MessageHelper.infoString( persister, persister.getIdentifier( entity, source ), source.getFactory() ) ); } if ( createCache.add( entity ) ) { return justCascade( createCache, source, entity, persister ); } return CompletionStages.nullFuture(); }
Example 2
Source File: DefaultPersistEventListener.java From lams with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings({"unchecked"}) private void entityIsDeleted(PersistEvent event, Map createCache) { final EventSource source = event.getSession(); final Object entity = source.getPersistenceContext().unproxy( event.getObject() ); final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity ); LOG.tracef( "un-scheduling entity deletion [%s]", MessageHelper.infoString( persister, persister.getIdentifier( entity, source ), source.getFactory() ) ); if ( createCache.put( entity, entity ) == null ) { justCascade( createCache, source, entity, persister ); } }
Example 3
Source File: DefaultReactivePersistEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 5 votes |
protected CompletionStage<Void> entityIsPersistent(PersistEvent event, IdentitySet createCache) { LOG.trace( "Ignoring persistent instance" ); final EventSource source = event.getSession(); //TODO: check that entry.getIdentifier().equals(requestedId) final Object entity = source.getPersistenceContextInternal().unproxy( event.getObject() ); final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity ); if ( createCache.add( entity ) ) { return justCascade( createCache, source, entity, persister ); } return CompletionStages.nullFuture(); }
Example 4
Source File: DefaultReactivePersistEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 5 votes |
/** * Handle the given create event. * * @param event The save event to be handled. * @param createCache The copy cache of entity instance to merge/copy instance. */ protected CompletionStage<Void> entityIsTransient(PersistEvent event, IdentitySet createCache) { LOG.trace( "Saving transient instance" ); final EventSource source = event.getSession(); final Object entity = source.getPersistenceContextInternal().unproxy( event.getObject() ); if ( createCache.add( entity ) ) { return reactiveSaveWithGeneratedId( entity, event.getEntityName(), createCache, source, false ) .thenApply( v -> null ); } return CompletionStages.nullFuture(); }
Example 5
Source File: DefaultPersistEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
@SuppressWarnings({"unchecked"}) protected void entityIsPersistent(PersistEvent event, Map createCache) { LOG.trace( "Ignoring persistent instance" ); final EventSource source = event.getSession(); //TODO: check that entry.getIdentifier().equals(requestedId) final Object entity = source.getPersistenceContext().unproxy( event.getObject() ); final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity ); if ( createCache.put( entity, entity ) == null ) { justCascade( createCache, source, entity, persister ); } }
Example 6
Source File: DefaultPersistEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Handle the given create event. * * @param event The save event to be handled. * @param createCache The copy cache of entity instance to merge/copy instance. */ @SuppressWarnings({"unchecked"}) protected void entityIsTransient(PersistEvent event, Map createCache) { LOG.trace( "Saving transient instance" ); final EventSource source = event.getSession(); final Object entity = source.getPersistenceContext().unproxy( event.getObject() ); if ( createCache.put( entity, entity ) == null ) { saveWithGeneratedId( entity, event.getEntityName(), createCache, source, false ); } }
Example 7
Source File: DefaultReactivePersistEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
/** * Handle the given create event. * * @param event The create event to be handled. */ public CompletionStage<Void> reactiveOnPersist(PersistEvent event, IdentitySet createCache) throws HibernateException { final SessionImplementor source = event.getSession(); final Object object = event.getObject(); final Object entity; if ( object instanceof HibernateProxy ) { LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); if ( li.isUninitialized() ) { if ( li.getSession() == source ) { return CompletionStages.nullFuture(); //NOTE EARLY EXIT! } else { return CompletionStages.failedFuture( new PersistentObjectException( "uninitialized proxy passed to persist()" ) ); } } entity = li.getImplementation(); } else { entity = object; } final String entityName; if ( event.getEntityName() != null ) { entityName = event.getEntityName(); } else { entityName = source.bestGuessEntityName( entity ); event.setEntityName( entityName ); } final EntityEntry entityEntry = source.getPersistenceContextInternal().getEntry( entity ); EntityState entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true ); if ( entityState == EntityState.DETACHED ) { // JPA 2, in its version of a "foreign generated", allows the id attribute value // to be manually set by the user, even though this manual value is irrelevant. // The issue is that this causes problems with the Hibernate unsaved-value strategy // which comes into play here in determining detached/transient state. // // Detect if we have this situation and if so null out the id value and calculate the // entity state again. // NOTE: entityEntry must be null to get here, so we cannot use any of its values EntityPersister persister = source.getFactory().getMetamodel().entityPersister( entityName ); if (persister.getIdentifierGenerator() instanceof ForeignGenerator) { if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) { LOG.debug( "Resetting entity id attribute to null for foreign generator" ); } persister.setIdentifier( entity, null, source ); entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true ); } } switch ( entityState ) { case DETACHED: { return CompletionStages.failedFuture( new PersistentObjectException( "detached entity passed to persist: " + EventUtil.getLoggableName( event.getEntityName(), entity ) ) ); } case PERSISTENT: { return entityIsPersistent( event, createCache ); } case TRANSIENT: { return entityIsTransient( event, createCache ); } case DELETED: { entityEntry.setStatus( Status.MANAGED ); entityEntry.setDeletedState( null ); event.getSession().getActionQueue().unScheduleDeletion( entityEntry, event.getObject() ); return entityIsDeleted( event, createCache ); } default: { return CompletionStages.failedFuture( new ObjectDeletedException( "deleted entity passed to persist", null, EventUtil.getLoggableName( event.getEntityName(), entity ) ) ); } } }
Example 8
Source File: DefaultPersistEventListener.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Handle the given create event. * * @param event The create event to be handled. * */ public void onPersist(PersistEvent event, Map createCache) throws HibernateException { final SessionImplementor source = event.getSession(); final Object object = event.getObject(); final Object entity; if ( object instanceof HibernateProxy ) { LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); if ( li.isUninitialized() ) { if ( li.getSession() == source ) { return; //NOTE EARLY EXIT! } else { throw new PersistentObjectException( "uninitialized proxy passed to persist()" ); } } entity = li.getImplementation(); } else { entity = object; } final String entityName; if ( event.getEntityName() != null ) { entityName = event.getEntityName(); } else { entityName = source.bestGuessEntityName( entity ); event.setEntityName( entityName ); } final EntityEntry entityEntry = source.getPersistenceContext().getEntry( entity ); EntityState entityState = getEntityState( entity, entityName, entityEntry, source ); if ( entityState == EntityState.DETACHED ) { // JPA 2, in its version of a "foreign generated", allows the id attribute value // to be manually set by the user, even though this manual value is irrelevant. // The issue is that this causes problems with the Hibernate unsaved-value strategy // which comes into play here in determining detached/transient state. // // Detect if we have this situation and if so null out the id value and calculate the // entity state again. // NOTE: entityEntry must be null to get here, so we cannot use any of its values EntityPersister persister = source.getFactory().getEntityPersister( entityName ); if ( ForeignGenerator.class.isInstance( persister.getIdentifierGenerator() ) ) { if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) { LOG.debug( "Resetting entity id attribute to null for foreign generator" ); } persister.setIdentifier( entity, null, source ); entityState = getEntityState( entity, entityName, entityEntry, source ); } } switch ( entityState ) { case DETACHED: { throw new PersistentObjectException( "detached entity passed to persist: " + getLoggableName( event.getEntityName(), entity ) ); } case PERSISTENT: { entityIsPersistent( event, createCache ); break; } case TRANSIENT: { entityIsTransient( event, createCache ); break; } case DELETED: { entityEntry.setStatus( Status.MANAGED ); entityEntry.setDeletedState( null ); event.getSession().getActionQueue().unScheduleDeletion( entityEntry, event.getObject() ); entityIsDeleted( event, createCache ); break; } default: { throw new ObjectDeletedException( "deleted entity passed to persist", null, getLoggableName( event.getEntityName(), entity ) ); } } }