Java Code Examples for org.hibernate.engine.spi.Status#READ_ONLY

The following examples show how to use org.hibernate.engine.spi.Status#READ_ONLY . 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: 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 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: 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 4
Source File: AbstractEntityEntry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AbstractEntityEntry(
		final Status status,
		final Object[] loadedState,
		final Object rowId,
		final Serializable id,
		final Object version,
		final LockMode lockMode,
		final boolean existsInDatabase,
		final EntityPersister persister,
		final boolean disableVersionIncrement,
		final PersistenceContext persistenceContext) {
	setCompressedValue( EnumState.STATUS, status );
	// not useful strictly speaking but more explicit
	setCompressedValue( EnumState.PREVIOUS_STATUS, null );
	// only retain loaded state if the status is not Status.READ_ONLY
	if ( status != Status.READ_ONLY ) {
		this.loadedState = loadedState;
	}
	this.id=id;
	this.rowId=rowId;
	setCompressedValue( BooleanState.EXISTS_IN_DATABASE, existsInDatabase );
	this.version=version;
	setCompressedValue( EnumState.LOCK_MODE, lockMode );
	setCompressedValue( BooleanState.IS_BEING_REPLICATED, disableVersionIncrement );
	this.persister=persister;
	this.persistenceContext = persistenceContext;
}
 
Example 5
Source File: AbstractEntityEntry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setStatus(Status status) {
	if ( status == Status.READ_ONLY ) {
		//memory optimization
		loadedState = null;
	}

	final Status currentStatus = this.getStatus();

	if ( currentStatus != status ) {
		setCompressedValue( EnumState.PREVIOUS_STATUS, currentStatus );
		setCompressedValue( EnumState.STATUS, status );
	}
}
 
Example 6
Source File: AbstractEntityEntry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isModifiableEntity() {
	final Status status = getStatus();
	final Status previousStatus = getPreviousStatus();
	return getPersister().isMutable()
			&& status != Status.READ_ONLY
			&& ! ( status == Status.DELETED && previousStatus == Status.READ_ONLY );
}
 
Example 7
Source File: AbstractEntityEntry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isReadOnly() {
	final Status status = getStatus();
	if (status != Status.MANAGED && status != Status.READ_ONLY) {
		throw new HibernateException("instance was not in a valid state");
	}
	return status == Status.READ_ONLY;
}
 
Example 8
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void checkNaturalId(
		EntityPersister persister,
		EntityEntry entry,
		Object[] current,
		Object[] loaded,
		SessionImplementor session) {
	if ( persister.hasNaturalIdentifier() && entry.getStatus() != Status.READ_ONLY ) {
		if ( !persister.getEntityMetamodel().hasImmutableNaturalId() ) {
			// SHORT-CUT: if the natural id is mutable (!immutable), no need to do the below checks
			// EARLY EXIT!!!
			return;
		}

		final int[] naturalIdentifierPropertiesIndexes = persister.getNaturalIdentifierProperties();
		final Type[] propertyTypes = persister.getPropertyTypes();
		final boolean[] propertyUpdateability = persister.getPropertyUpdateability();

		final Object[] snapshot = loaded == null
				? session.getPersistenceContext().getNaturalIdSnapshot( entry.getId(), persister )
				: session.getPersistenceContext().getNaturalIdHelper().extractNaturalIdValues( loaded, persister );

		for ( int i = 0; i < naturalIdentifierPropertiesIndexes.length; i++ ) {
			final int naturalIdentifierPropertyIndex = naturalIdentifierPropertiesIndexes[i];
			if ( propertyUpdateability[naturalIdentifierPropertyIndex] ) {
				// if the given natural id property is updatable (mutable), there is nothing to check
				continue;
			}

			final Type propertyType = propertyTypes[naturalIdentifierPropertyIndex];
			if ( !propertyType.isEqual( current[naturalIdentifierPropertyIndex], snapshot[i] ) ) {
				throw new HibernateException(
						String.format(
								"An immutable natural identifier of entity %s was altered from %s to %s",
								persister.getEntityName(),
								propertyTypes[naturalIdentifierPropertyIndex].toLoggableString(
										snapshot[i],
										session.getFactory()
								),
								propertyTypes[naturalIdentifierPropertyIndex].toLoggableString(
										current[naturalIdentifierPropertyIndex],
										session.getFactory()
								)
						)
				);
			}
		}
	}
}
 
Example 9
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private boolean isCollectionDirtyCheckNecessary(EntityPersister persister, Status status) {
	return ( status == Status.MANAGED || status == Status.READ_ONLY ) &&
			persister.isVersioned() &&
			persister.hasCollections();
}
 
Example 10
Source File: UnresolvedEntityInsertActions.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Resolve any dependencies on {@code managedEntity}.
 *
 * @param managedEntity - the managed entity name
 * @param session - the session
 *
 * @return the insert actions that depended only on the specified entity.
 *
 * @throws IllegalArgumentException if {@code managedEntity} did not have managed or read-only status.
 */
@SuppressWarnings({ "unchecked" })
public Set<AbstractEntityInsertAction> resolveDependentActions(Object managedEntity, SessionImplementor session) {
	final EntityEntry entityEntry = session.getPersistenceContext().getEntry( managedEntity );
	if ( entityEntry.getStatus() != Status.MANAGED && entityEntry.getStatus() != Status.READ_ONLY ) {
		throw new IllegalArgumentException( "EntityEntry did not have status MANAGED or READ_ONLY: " + entityEntry );
	}

	final boolean traceEnabled = LOG.isTraceEnabled();
	// Find out if there are any unresolved insertions that are waiting for the
	// specified entity to be resolved.
	final Set<AbstractEntityInsertAction> dependentActions = dependentActionsByTransientEntity.remove( managedEntity );
	if ( dependentActions == null ) {
		if ( traceEnabled ) {
			LOG.tracev(
					"No unresolved entity inserts that depended on [{0}]",
					MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() )
			);
		}
		// NOTE EARLY EXIT!
		return Collections.emptySet();
	}
	final Set<AbstractEntityInsertAction> resolvedActions = new IdentitySet(  );
	if ( traceEnabled  ) {
		LOG.tracev(
				"Unresolved inserts before resolving [{0}]: [{1}]",
				MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ),
				toString()
		);
	}
	for ( AbstractEntityInsertAction dependentAction : dependentActions ) {
		if ( traceEnabled ) {
			LOG.tracev(
					"Resolving insert [{0}] dependency on [{1}]",
					MessageHelper.infoString( dependentAction.getEntityName(), dependentAction.getId() ),
					MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() )
			);
		}
		final NonNullableTransientDependencies dependencies = dependenciesByAction.get( dependentAction );
		dependencies.resolveNonNullableTransientEntity( managedEntity );
		if ( dependencies.isEmpty() ) {
			if ( traceEnabled ) {
				LOG.tracev(
						"Resolving insert [{0}] (only depended on [{1}])",
						dependentAction,
						MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() )
				);
			}
			// dependentAction only depended on managedEntity..
			dependenciesByAction.remove( dependentAction );
			resolvedActions.add( dependentAction );
		}
	}
	if ( traceEnabled  ) {
		LOG.tracev(
				"Unresolved inserts after resolving [{0}]: [{1}]",
				MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ),
				toString()
		);
	}
	return resolvedActions;
}