org.hibernate.engine.spi.EntityEntry Java Examples

The following examples show how to use org.hibernate.engine.spi.EntityEntry. 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: OptimisticLockingChildUpdatesRootVersionTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
private boolean updated(FlushEntityEvent event) {
    final EntityEntry entry = event.getEntityEntry();
    final Object entity = event.getEntity();

    int[] dirtyProperties;
    EntityPersister persister = entry.getPersister();
    final Object[] values = event.getPropertyValues();
    SessionImplementor session = event.getSession();

    if ( event.hasDatabaseSnapshot() ) {
        dirtyProperties = persister.findModified( event.getDatabaseSnapshot(), values, entity, session );
    }
    else {
        dirtyProperties = persister.findDirty( values, entry.getLoadedState(), entity, session );
    }

    return dirtyProperties != null;
}
 
Example #2
Source File: AbstractFlushingEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
	 * process cascade save/update at the start of a flush to discover
	 * any newly referenced entity that must be passed to saveOrUpdate(),
	 * and also apply orphan delete
	 */
	private void prepareEntityFlushes(EventSource session, PersistenceContext persistenceContext) throws HibernateException {

		LOG.debug( "Processing flush-time cascades" );

		final Object anything = getAnything();
		//safe from concurrent modification because of how concurrentEntries() is implemented on IdentityMap
		for ( Map.Entry<Object,EntityEntry> me : persistenceContext.reentrantSafeEntityEntries() ) {
//		for ( Map.Entry me : IdentityMap.concurrentEntries( persistenceContext.getEntityEntries() ) ) {
			EntityEntry entry = (EntityEntry) me.getValue();
			Status status = entry.getStatus();
			if ( status == Status.MANAGED || status == Status.SAVING || status == Status.READ_ONLY ) {
				cascadeOnFlush( session, entry.getPersister(), me.getKey(), anything );
			}
		}
	}
 
Example #3
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
	public String getEntityName(Object object) {
		checkOpen();
//		checkTransactionSynchStatus();
		if ( object instanceof HibernateProxy ) {
			if ( !persistenceContext.containsProxy( object ) ) {
				throw new TransientObjectException( "proxy was not associated with the session" );
			}
			object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation();
		}

		EntityEntry entry = persistenceContext.getEntry( object );
		if ( entry == null ) {
			throwTransientObjectException( object );
		}
		return entry.getPersister().getEntityName();
	}
 
Example #4
Source File: ForumServiceImpl.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional(readOnly = true)
public List<Post> findAllByTitle(String title) {
    List<Post> posts = postDAO.findByTitle(title);

    org.hibernate.engine.spi.PersistenceContext persistenceContext = getHibernatePersistenceContext();

    for(Post post : posts) {
        assertTrue(entityManager.contains(post));

        EntityEntry entityEntry = persistenceContext.getEntry(post);
        assertNull(entityEntry.getLoadedState());
    }

    return posts;
}
 
Example #5
Source File: GrailsHibernateUtil.java    From gorm-hibernate5 with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the target object to read-write, allowing Hibernate to dirty check it and auto-flush changes.
 *
 * @see #setObjectToReadyOnly(Object, org.hibernate.SessionFactory)
 *
 * @param target The target object
 * @param sessionFactory The SessionFactory instance
 */
public static void setObjectToReadWrite(final Object target, SessionFactory sessionFactory) {
    Session session = sessionFactory.getCurrentSession();
    if (!canModifyReadWriteState(session, target)) {
        return;
    }

    SessionImplementor sessionImpl = (SessionImplementor) session;
    EntityEntry ee = sessionImpl.getPersistenceContext().getEntry(target);

    if (ee == null || ee.getStatus() != Status.READ_ONLY) {
        return;
    }

    Object actualTarget = target;
    if (target instanceof HibernateProxy) {
        actualTarget = ((HibernateProxy)target).getHibernateLazyInitializer().getImplementation();
    }

    session.setReadOnly(actualTarget, false);
    session.setHibernateFlushMode(FlushMode.AUTO);
    incrementVersion(target);
}
 
Example #6
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
	checkOpen();
	checkTransactionSynchStatus();
	if ( object instanceof HibernateProxy ) {
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.getSession() != this ) {
			throw new TransientObjectException( "The proxy was not associated with this session" );
		}
		return li.getIdentifier();
	}
	else {
		EntityEntry entry = persistenceContext.getEntry( object );
		if ( entry == null ) {
			throw new TransientObjectException( "The instance was not associated with this session" );
		}
		return entry.getId();
	}
}
 
Example #7
Source File: EntityAction.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * entity id accessor
 *
 * @return The entity id
 */
public final Serializable getId() {
	if ( id instanceof DelayedPostInsertIdentifier ) {
		final EntityEntry entry = session.getPersistenceContext().getEntry( instance );
		if ( entry == null ) {
			if ( LOG.isDebugEnabled() ) {
				LOG.debugf(
						"Skipping action - the persistence context does not contain any entry for the entity [%s]. This may occur if an entity is created and then deleted in the same transaction/flush.",
						instance
				);
			}
			// If an Entity is created and then deleted in the same Transaction, when Action#postDelete() calls this method the persistence context
			// does not contain anymore an entry.
			return null;
		}
		final Serializable eeId = entry.getId();
		return eeId instanceof DelayedPostInsertIdentifier ? null : eeId;
	}
	return id;
}
 
Example #8
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Object[] getValues(Object entity, EntityEntry entry, boolean mightBeDirty, SessionImplementor session) {
	final Object[] loadedState = entry.getLoadedState();
	final Status status = entry.getStatus();
	final EntityPersister persister = entry.getPersister();

	final Object[] values;
	if ( status == Status.DELETED ) {
		//grab its state saved at deletion
		values = entry.getDeletedState();
	}
	else if ( !mightBeDirty && loadedState != null ) {
		values = loadedState;
	}
	else {
		checkId( entity, persister, entry.getId(), session );

		// grab its current state
		values = persister.getPropertyValues( entity );

		checkNaturalId( persister, entry, values, loadedState, session );
	}
	return values;
}
 
Example #9
Source File: AbstractReactiveFlushingEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * process cascade save/update at the start of a flush to discover
 * any newly referenced entity that must be passed to saveOrUpdate(),
 * and also apply orphan delete
 */
private CompletionStage<Void> prepareEntityFlushes(EventSource session, PersistenceContext persistenceContext) throws HibernateException {

	LOG.debug( "Processing flush-time cascades" );

	CompletionStage<Void> stage = CompletionStages.nullFuture();
	final IdentitySet copiedAlready = new IdentitySet( 10 );
	//safe from concurrent modification because of how concurrentEntries() is implemented on IdentityMap
	for ( Map.Entry<Object, EntityEntry> me : persistenceContext.reentrantSafeEntityEntries() ) {
		EntityEntry entry = me.getValue();
		Status status = entry.getStatus();
		if ( status == Status.MANAGED || status == Status.SAVING || status == Status.READ_ONLY ) {
			stage = stage.thenCompose( v -> cascadeOnFlush( session, entry.getPersister(), me.getKey(), copiedAlready ) );
		}
	}
	return stage;
}
 
Example #10
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public LockMode getCurrentLockMode(Object object) throws HibernateException {
	checkOpen();
	checkTransactionSynchStatus();
	if ( object == null ) {
		throw new NullPointerException( "null object passed to getCurrentLockMode()" );
	}
	if ( object instanceof HibernateProxy ) {
		object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation( this );
		if ( object == null ) {
			return LockMode.NONE;
		}
	}
	EntityEntry e = persistenceContext.getEntry( object );
	if ( e == null ) {
		throw new TransientObjectException( "Given object not associated with the session" );
	}
	if ( e.getStatus() != Status.MANAGED ) {
		throw new ObjectDeletedException(
				"The given object was deleted",
				e.getId(),
				e.getPersister().getEntityName()
		);
	}
	return e.getLockMode();
}
 
Example #11
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected boolean invokeInterceptor(
		SessionImplementor session,
		Object entity,
		EntityEntry entry,
		final Object[] values,
		EntityPersister persister) {
	boolean isDirty = false;
	if ( entry.getStatus() != Status.DELETED ) {
		if ( callbackRegistry.preUpdate( entity ) ) {
			isDirty = copyState( entity, persister.getPropertyTypes(), values, session.getFactory() );
		}
	}

	final boolean answerFromInterceptor =  session.getInterceptor().onFlushDirty(
			entity,
			entry.getId(),
			values,
			entry.getLoadedState(),
			persister.getPropertyNames(),
			persister.getPropertyTypes()
	);

	return answerFromInterceptor || isDirty;
}
 
Example #12
Source File: MutableEntityEntry.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Custom deserialization routine used during deserialization of a
 * Session/PersistenceContext for increased performance.
 *
 * @param ois The stream from which to read the entry.
 * @param persistenceContext The context being deserialized.
 *
 * @return The deserialized EntityEntry
 *
 * @throws java.io.IOException If a stream error occurs
 * @throws ClassNotFoundException If any of the classes declared in the stream
 * cannot be found
 */
public static EntityEntry deserialize(
		ObjectInputStream ois,
		PersistenceContext persistenceContext) throws IOException, ClassNotFoundException {
	String previousStatusString;
	return new MutableEntityEntry(
			persistenceContext.getSession().getFactory(),
			(String) ois.readObject(),
			(Serializable) ois.readObject(),
			Status.valueOf( (String) ois.readObject() ),
			( previousStatusString = (String) ois.readObject() ).length() == 0
					? null
					: Status.valueOf( previousStatusString ),
			(Object[]) ois.readObject(),
			(Object[]) ois.readObject(),
			ois.readObject(),
			LockMode.valueOf( (String) ois.readObject() ),
			ois.readBoolean(),
			ois.readBoolean(),
			persistenceContext
	);
}
 
Example #13
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public EntityEntry addEntity(
		final Object entity,
		final Status status,
		final Object[] loadedState,
		final EntityKey entityKey,
		final Object version,
		final LockMode lockMode,
		final boolean existsInDatabase,
		final EntityPersister persister,
		final boolean disableVersionIncrement) {
	addEntity( entityKey, entity );
	return addEntry(
			entity,
			status,
			loadedState,
			null,
			entityKey.getIdentifier(),
			version,
			lockMode,
			existsInDatabase,
			persister,
			disableVersionIncrement
	);
}
 
Example #14
Source File: OptimisticLockingBidirectionalChildUpdatesRootVersionTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
private boolean updated(FlushEntityEvent event) {
    final EntityEntry entry = event.getEntityEntry();
    final Object entity = event.getEntity();

    int[] dirtyProperties;
    EntityPersister persister = entry.getPersister();
    final Object[] values = event.getPropertyValues();
    SessionImplementor session = event.getSession();

    if ( event.hasDatabaseSnapshot() ) {
        dirtyProperties = persister.findModified( event.getDatabaseSnapshot(), values, entity, session );
    }
    else {
        dirtyProperties = persister.findDirty( values, entry.getLoadedState(), entity, session );
    }

    return dirtyProperties != null;
}
 
Example #15
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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()
	);
}
 
Example #16
Source File: ForumServiceImpl.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional(readOnly = true)
public List<Post> findAllByTitle(String title) {
    List<Post> posts = postDAO.findByTitle(title);

    Session session = sessionFactory.getCurrentSession();
    PersistenceContext persistenceContext = ((SharedSessionContractImplementor) session)
            .getPersistenceContext();

    for(Post post : posts) {
        assertTrue(session.contains(post));

        EntityEntry entityEntry = persistenceContext.getEntry(post);
        assertNull(entityEntry.getLoadedState());
    }

    return posts;
}
 
Example #17
Source File: TwoPhaseLoad.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * PostLoad cannot occur during initializeEntity, as that call occurs *before*
 * the Set collections are added to the persistence context by Loader.
 * Without the split, LazyInitializationExceptions can occur in the Entity's
 * postLoad if it acts upon the collection.
 *
 * HHH-6043
 *
 * @param entity The entity
 * @param session The Session
 * @param postLoadEvent The (re-used) post-load event
 */
public static void postLoad(
		final Object entity,
		final SharedSessionContractImplementor session,
		final PostLoadEvent postLoadEvent) {

	if ( session.isEventSource() ) {
		final PersistenceContext persistenceContext
				= session.getPersistenceContext();
		final EntityEntry entityEntry = persistenceContext.getEntry( entity );

		postLoadEvent.setEntity( entity ).setId( entityEntry.getId() ).setPersister( entityEntry.getPersister() );

		final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory()
						.getServiceRegistry()
						.getService( EventListenerRegistry.class )
						.getEventListenerGroup( EventType.POST_LOAD );
		for ( PostLoadEventListener listener : listenerGroup.listeners() ) {
			listener.onPostLoad( postLoadEvent );
		}
	}
}
 
Example #18
Source File: MutableEntityEntryFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public EntityEntry createEntityEntry(
		Status status,
		Object[] loadedState,
		Object rowId,
		Serializable id,
		Object version,
		LockMode lockMode,
		boolean existsInDatabase,
		EntityPersister persister,
		boolean disableVersionIncrement,
		PersistenceContext persistenceContext) {
	return new MutableEntityEntry(
			status,
			loadedState,
			rowId,
			id,
			version,
			lockMode,
			existsInDatabase,
			persister,
			disableVersionIncrement,
			persistenceContext
	);
}
 
Example #19
Source File: AbstractReactiveFlushingEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 1. detect any dirty entities
 * 2. schedule any entity updates
 * 3. search out any reachable collections
 */
private int flushEntities(final FlushEvent event, final PersistenceContext persistenceContext) throws HibernateException {

	LOG.trace( "Flushing entities and processing referenced collections" );

	final EventSource source = event.getSession();
	final Iterable<FlushEntityEventListener> flushListeners =
			source.getFactory().getServiceRegistry()
					.getService( EventListenerRegistry.class )
					.getEventListenerGroup( EventType.FLUSH_ENTITY )
					.listeners();

	// Among other things, updateReachables() will recursively load all
	// collections that are moving roles. This might cause entities to
	// be loaded.

	// So this needs to be safe from concurrent modification problems.

	final Map.Entry<Object,EntityEntry>[] entityEntries = persistenceContext.reentrantSafeEntityEntries();
	final int count = entityEntries.length;

	for ( Map.Entry<Object,EntityEntry> me : entityEntries ) {

		// Update the status of the object and if necessary, schedule an update

		EntityEntry entry = me.getValue();
		Status status = entry.getStatus();

		if ( status != Status.LOADING && status != Status.GONE ) {
			final FlushEntityEvent entityEvent = new FlushEntityEvent( source, me.getKey(), entry );
			for ( FlushEntityEventListener listener : flushListeners ) {
				listener.onFlushEntity( entityEvent );
			}
		}
	}

	source.getActionQueue().sortActions();

	return count;
}
 
Example #20
Source File: AbstractFlushingEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 1. detect any dirty entities
 * 2. schedule any entity updates
 * 3. search out any reachable collections
 */
private int flushEntities(final FlushEvent event, final PersistenceContext persistenceContext) throws HibernateException {

	LOG.trace( "Flushing entities and processing referenced collections" );

	final EventSource source = event.getSession();
	final Iterable<FlushEntityEventListener> flushListeners = source.getFactory().getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( EventType.FLUSH_ENTITY )
			.listeners();

	// Among other things, updateReachables() will recursively load all
	// collections that are moving roles. This might cause entities to
	// be loaded.

	// So this needs to be safe from concurrent modification problems.

	final Map.Entry<Object,EntityEntry>[] entityEntries = persistenceContext.reentrantSafeEntityEntries();
	final int count = entityEntries.length;

	for ( Map.Entry<Object,EntityEntry> me : entityEntries ) {

		// Update the status of the object and if necessary, schedule an update

		EntityEntry entry = me.getValue();
		Status status = entry.getStatus();

		if ( status != Status.LOADING && status != Status.GONE ) {
			final FlushEntityEvent entityEvent = new FlushEntityEvent( source, me.getKey(), entry );
			for ( FlushEntityEventListener listener : flushListeners ) {
				listener.onFlushEntity( entityEvent );
			}
		}
	}

	source.getActionQueue().sortActions();

	return count;
}
 
Example #21
Source File: AbstractEntityInsertAction.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterDeserialize(SharedSessionContractImplementor session) {
	super.afterDeserialize( session );
	// IMPL NOTE: non-flushed changes code calls this method with session == null...
	// guard against NullPointerException
	if ( session != null ) {
		final EntityEntry entityEntry = session.getPersistenceContext().getEntry( getInstance() );
		this.state = entityEntry.getLoadedState();
	}
}
 
Example #22
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void setEntityReadOnly(Object entity, boolean readOnly) {
	final EntityEntry entry = getEntry( entity );
	if ( entry == null ) {
		throw new TransientObjectException( "Instance was not associated with this persistence context" );
	}
	entry.setReadOnly( readOnly, entity );
	hasNonReadOnlyEntities = hasNonReadOnlyEntities || ! readOnly;
}
 
Example #23
Source File: OptimisticLockingStrategy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
	if ( !lockable.isVersioned() ) {
		throw new OptimisticLockException( object, "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
	}
	final EntityEntry entry = session.getPersistenceContext().getEntry( object );
	// Register the EntityVerifyVersionProcess action to run just prior to transaction commit.
	( (EventSource) session ).getActionQueue().registerProcess( new EntityVerifyVersionProcess( object, entry ) );
}
 
Example #24
Source File: PostUpdateEventListenerStandardImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void handlePostUpdate(Object entity, EventSource source) {
	EntityEntry entry = source.getPersistenceContext().getEntry( entity );
	// mimic the preUpdate filter
	if ( Status.DELETED != entry.getStatus()) {
		callbackRegistry.postUpdate(entity);
	}
}
 
Example #25
Source File: ReactiveJoinedSubclassEntityPersister.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public boolean initializeLazyProperty(String fieldName, Object entity,
									  SharedSessionContractImplementor session,
									  EntityEntry entry,
									  int lazyIndex,
									  Object selectedValue) {
	return super.initializeLazyProperty(fieldName, entity, session, entry, lazyIndex, selectedValue);
}
 
Example #26
Source File: ForumServiceImpl.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public Post findById(Long id) {
    Post post = postDAO.findById(id);

    org.hibernate.engine.spi.PersistenceContext persistenceContext = getHibernatePersistenceContext();

    EntityEntry entityEntry = persistenceContext.getEntry(post);
    assertNotNull(entityEntry.getLoadedState());

    return post;
}
 
Example #27
Source File: ReactiveSingleTableEntityPersister.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public boolean initializeLazyProperty(String fieldName, Object entity,
									  SharedSessionContractImplementor session,
									  EntityEntry entry,
									  int lazyIndex,
									  Object selectedValue) {
	return super.initializeLazyProperty(fieldName, entity, session, entry, lazyIndex, selectedValue);
}
 
Example #28
Source File: DefaultSaveEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) {
	// this implementation is supposed to tolerate incorrect unsaved-value
	// mappings, for the purpose of backward-compatibility
	EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() );
	if ( entry!=null && entry.getStatus() != Status.DELETED ) {
		return entityIsPersistent(event);
	}
	else {
		return entityIsTransient(event);
	}
}
 
Example #29
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object proxyFor(Object impl) throws HibernateException {
	final EntityEntry e = getEntry( impl );
	if ( e == null ) {
		return impl;
	}
	return proxyFor( e.getPersister(), e.getEntityKey(), impl );
}
 
Example #30
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public EntityEntry addReferenceEntry(
		final Object entity,
		final Status status) {

	((ManagedEntity)entity).$$_hibernate_getEntityEntry().setStatus( status );
	entityEntryContext.addEntityEntry( entity, ((ManagedEntity)entity).$$_hibernate_getEntityEntry() );

	setHasNonReadOnlyEnties( status );
	return ((ManagedEntity)entity).$$_hibernate_getEntityEntry();
}