Java Code Examples for org.hibernate.persister.entity.EntityPersister#getVersion()

The following examples show how to use org.hibernate.persister.entity.EntityPersister#getVersion() . 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: StatelessSessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void update(String entityName, Object entity) {
	checkOpen();
	EntityPersister persister = getEntityPersister( entityName, entity );
	Serializable id = persister.getIdentifier( entity, this );
	Object[] state = persister.getPropertyValues( entity );
	Object oldVersion;
	if ( persister.isVersioned() ) {
		oldVersion = persister.getVersion( entity );
		Object newVersion = Versioning.increment( oldVersion, persister.getVersionType(), this );
		Versioning.setVersion( state, newVersion, persister );
		persister.setPropertyValues( entity, state );
	}
	else {
		oldVersion = null;
	}
	persister.update( id, state, null, false, null, oldVersion, entity, null, this );
}
 
Example 2
Source File: StatelessSessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void update(String entityName, Object entity) {
	errorIfClosed();
	EntityPersister persister = getEntityPersister(entityName, entity);
	Serializable id = persister.getIdentifier(entity, EntityMode.POJO);
	Object[] state = persister.getPropertyValues(entity, EntityMode.POJO);
	Object oldVersion;
	if ( persister.isVersioned() ) {
		oldVersion = persister.getVersion(entity, EntityMode.POJO);
		Object newVersion = Versioning.increment( oldVersion, persister.getVersionType(), this );
		Versioning.setVersion(state, newVersion, persister);
		persister.setPropertyValues(entity, state, EntityMode.POJO);
	}
	else {
		oldVersion = null;
	}
	persister.update(id, state, null, false, null, oldVersion, entity, null, this);
}
 
Example 3
Source File: StatelessSessionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void delete(String entityName, Object entity) {
	checkOpen();
	EntityPersister persister = getEntityPersister( entityName, entity );
	Serializable id = persister.getIdentifier( entity, this );
	Object version = persister.getVersion( entity );
	persister.delete( id, version, entity, this );
}
 
Example 4
Source File: StatelessSessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void delete(String entityName, Object entity) {
	errorIfClosed();
	EntityPersister persister = getEntityPersister(entityName, entity);
	Serializable id = persister.getIdentifier(entity, EntityMode.POJO);
	Object version = persister.getVersion(entity, EntityMode.POJO);
	persister.delete(id, version, entity, this);
}
 
Example 5
Source File: ReactiveEntityDeleteAction.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public CompletionStage<Void> reactiveExecute() throws HibernateException {
	final Serializable id = getId();
	final EntityPersister persister = getPersister();
	final SharedSessionContractImplementor session = getSession();
	final Object instance = getInstance();

	final boolean veto = preDelete();

	Object version = getVersion();
	if ( persister.isVersionPropertyGenerated() ) {
		// we need to grab the version value from the entity, otherwise
		// we have issues with generated-version entities that may have
		// multiple actions queued during the same flush
		version = persister.getVersion( instance );
	}

	final Object ck;
	if ( persister.canWriteToCache() ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		ck = cache.generateCacheKey( id, persister, session.getFactory(), session.getTenantIdentifier() );
		setLock( cache.lockItem( session, ck, version ) );
	}
	else {
		ck = null;
	}

	CompletionStage<?> deleteStep = CompletionStages.nullFuture();
	if ( !isCascadeDeleteEnabled() && !veto ) {
		deleteStep = ((ReactiveEntityPersister) persister).deleteReactive( id, version, instance, session );
	}

	return deleteStep.thenAccept( deleteAR -> {
		//postDelete:
		// After actually deleting a row, record the fact that the instance no longer
		// exists on the database (needed for identity-column key generation), and
		// remove it from the session cache
		final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
		final EntityEntry entry = persistenceContext.removeEntry( instance );
		if ( entry == null ) {
			throw new AssertionFailure( "possible non-threadsafe access to session" );
		}
		entry.postDelete();

		persistenceContext.removeEntity( entry.getEntityKey() );
		persistenceContext.removeProxy( entry.getEntityKey() );

		if ( persister.canWriteToCache() ) {
			persister.getCacheAccessStrategy().remove( session, ck );
		}

		persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference(
				persister,
				id,
				getNaturalIdValues()
		);

		postDelete();

		final StatisticsImplementor statistics = getSession().getFactory().getStatistics();
		if ( statistics.isStatisticsEnabled() && !veto ) {
			statistics.deleteEntity( getPersister().getEntityName() );
		}
	} );
}
 
Example 6
Source File: DefaultDeleteEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Handle the given delete event.  This is the cascaded form.
 *
 * @param event The delete event.
 * @param transientEntities The cache of entities already deleted
 *
 * @throws HibernateException
 */
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {

	final EventSource source = event.getSession();

	final PersistenceContext persistenceContext = source.getPersistenceContext();
	Object entity = persistenceContext.unproxyAndReassociate( event.getObject() );

	EntityEntry entityEntry = persistenceContext.getEntry( entity );
	final EntityPersister persister;
	final Serializable id;
	final Object version;

	if ( entityEntry == null ) {
		LOG.trace( "Entity was not persistent in delete processing" );

		persister = source.getEntityPersister( event.getEntityName(), entity );

		if ( ForeignKeys.isTransient( persister.getEntityName(), entity, null, source ) ) {
			deleteTransientEntity( source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities );
			// EARLY EXIT!!!
			return;
		}
		performDetachedEntityDeletionCheck( event );

		id = persister.getIdentifier( entity, source );

		if ( id == null ) {
			throw new TransientObjectException(
					"the detached instance passed to delete() had a null identifier"
			);
		}

		final EntityKey key = source.generateEntityKey( id, persister );

		persistenceContext.checkUniqueness( key, entity );

		new OnUpdateVisitor( source, id, entity ).process( entity, persister );

		version = persister.getVersion( entity );

		entityEntry = persistenceContext.addEntity(
				entity,
				(persister.isMutable() ? Status.MANAGED : Status.READ_ONLY),
				persister.getPropertyValues( entity ),
				key,
				version,
				LockMode.NONE,
				true,
				persister,
				false
		);
	}
	else {
		LOG.trace( "Deleting a persistent instance" );

		if ( entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE ) {
			LOG.trace( "Object was already deleted" );
			return;
		}
		persister = entityEntry.getPersister();
		id = entityEntry.getId();
		version = entityEntry.getVersion();
	}

	/*if ( !persister.isMutable() ) {
		throw new HibernateException(
				"attempted to delete an object of immutable class: " +
				MessageHelper.infoString(persister)
			);
	}*/

	if ( invokeDeleteLifecycle( source, entity, persister ) ) {
		return;
	}

	deleteEntity(
			source,
			entity,
			entityEntry,
			event.isCascadeDeleteEnabled(),
			event.isOrphanRemovalBeforeUpdates(),
			persister,
			transientEntities
	);

	if ( source.getFactory().getSettings().isIdentifierRollbackEnabled() ) {
		persister.resetIdentifier( entity, id, version, source );
	}
}
 
Example 7
Source File: EntityDeleteAction.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void execute() throws HibernateException {
	final Serializable id = getId();
	final EntityPersister persister = getPersister();
	final SharedSessionContractImplementor session = getSession();
	final Object instance = getInstance();

	final boolean veto = preDelete();

	Object version = this.version;
	if ( persister.isVersionPropertyGenerated() ) {
		// we need to grab the version value from the entity, otherwise
		// we have issues with generated-version entities that may have
		// multiple actions queued during the same flush
		version = persister.getVersion( instance );
	}

	final Object ck;
	if ( persister.canWriteToCache() ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		ck = cache.generateCacheKey( id, persister, session.getFactory(), session.getTenantIdentifier() );
		lock = cache.lockItem( session, ck, version );
	}
	else {
		ck = null;
	}

	if ( !isCascadeDeleteEnabled && !veto ) {
		persister.delete( id, version, instance, session );
	}
	
	//postDelete:
	// After actually deleting a row, record the fact that the instance no longer 
	// exists on the database (needed for identity-column key generation), and
	// remove it from the session cache
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final EntityEntry entry = persistenceContext.removeEntry( instance );
	if ( entry == null ) {
		throw new AssertionFailure( "possible nonthreadsafe access to session" );
	}
	entry.postDelete();

	persistenceContext.removeEntity( entry.getEntityKey() );
	persistenceContext.removeProxy( entry.getEntityKey() );
	
	if ( persister.canWriteToCache() ) {
		persister.getCacheAccessStrategy().remove( session, ck);
	}

	persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference( persister, id, naturalIdValues );

	postDelete();

	if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
		getSession().getFactory().getStatistics().deleteEntity( getPersister().getEntityName() );
	}
}
 
Example 8
Source File: DefaultDeleteEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Handle the given delete event.  This is the cascaded form.
 *
 * @param event The delete event.
 * @param transientEntities The cache of entities already deleted
 *
 * @throws HibernateException
 */
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {

	final EventSource source = event.getSession();

	final PersistenceContext persistenceContext = source.getPersistenceContext();
	Object entity = persistenceContext.unproxyAndReassociate( event.getObject() );

	EntityEntry entityEntry = persistenceContext.getEntry( entity );
	final EntityPersister persister;
	final Serializable id;
	final Object version;

	if ( entityEntry == null ) {
		log.trace( "entity was not persistent in delete processing" );

		persister = source.getEntityPersister( event.getEntityName(), entity );

		if ( ForeignKeys.isTransient( persister.getEntityName(), entity, null, source ) ) {
			deleteTransientEntity( source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities );
			// EARLY EXIT!!!
			return;
		}
		else {
			performDetachedEntityDeletionCheck( event );
		}

		id = persister.getIdentifier( entity, source.getEntityMode() );

		if ( id == null ) {
			throw new TransientObjectException(
					"the detached instance passed to delete() had a null identifier"
			);
		}

		EntityKey key = new EntityKey( id, persister, source.getEntityMode() );

		persistenceContext.checkUniqueness( key, entity );

		new OnUpdateVisitor( source, id, entity ).process( entity, persister );

		version = persister.getVersion( entity, source.getEntityMode() );

		entityEntry = persistenceContext.addEntity(
				entity,
				Status.MANAGED,
				persister.getPropertyValues( entity, source.getEntityMode() ),
				key,
				version,
				LockMode.NONE,
				true,
				persister,
				false,
				false
		);
	}
	else {
		log.trace( "deleting a persistent instance" );

		if ( entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE ) {
			log.trace( "object was already deleted" );
			return;
		}
		persister = entityEntry.getPersister();
		id = entityEntry.getId();
		version = entityEntry.getVersion();
	}

	/*if ( !persister.isMutable() ) {
		throw new HibernateException(
				"attempted to delete an object of immutable class: " +
				MessageHelper.infoString(persister)
			);
	}*/

	if ( invokeDeleteLifecycle( source, entity, persister ) ) {
		return;
	}

	deleteEntity( source, entity, entityEntry, event.isCascadeDeleteEnabled(), persister, transientEntities );

	if ( source.getFactory().getSettings().isIdentifierRollbackEnabled() ) {
		persister.resetIdentifier( entity, id, version, source.getEntityMode() );
	}
}
 
Example 9
Source File: EntityDeleteAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void execute() throws HibernateException {
	Serializable id = getId();
	EntityPersister persister = getPersister();
	SessionImplementor session = getSession();
	Object instance = getInstance();

	boolean veto = preDelete();

	Object version = this.version;
	if ( persister.isVersionPropertyGenerated() ) {
		// we need to grab the version value from the entity, otherwise
		// we have issues with generated-version entities that may have
		// multiple actions queued during the same flush
		version = persister.getVersion( instance, session.getEntityMode() );
	}

	final CacheKey ck;
	if ( persister.hasCache() ) {
		ck = new CacheKey( 
				id, 
				persister.getIdentifierType(), 
				persister.getRootEntityName(), 
				session.getEntityMode(), 
				session.getFactory() 
			);
		lock = persister.getCache().lock(ck, version);
	}
	else {
		ck = null;
	}

	if ( !isCascadeDeleteEnabled && !veto ) {
		persister.delete( id, version, instance, session );
	}
	
	//postDelete:
	// After actually deleting a row, record the fact that the instance no longer 
	// exists on the database (needed for identity-column key generation), and
	// remove it from the session cache
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	EntityEntry entry = persistenceContext.removeEntry( instance );
	if ( entry == null ) {
		throw new AssertionFailure( "possible nonthreadsafe access to session" );
	}
	entry.postDelete();

	EntityKey key = new EntityKey( entry.getId(), entry.getPersister(), session.getEntityMode() );
	persistenceContext.removeEntity(key);
	persistenceContext.removeProxy(key);
	
	if ( persister.hasCache() ) persister.getCache().evict(ck);

	postDelete();

	if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
		getSession().getFactory().getStatisticsImplementor()
				.deleteEntity( getPersister().getEntityName() );
	}
}