org.hibernate.persister.entity.EntityPersister Java Examples

The following examples show how to use org.hibernate.persister.entity.EntityPersister. 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: DefaultMergeEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void copyValues(
	final EntityPersister persister, 
	final Object entity, 
	final Object target, 
	final SessionImplementor source,
	final Map copyCache
) {
	
	final Object[] copiedValues = TypeFactory.replace(
			persister.getPropertyValues( entity, source.getEntityMode() ),
			persister.getPropertyValues( target, source.getEntityMode() ),
			persister.getPropertyTypes(),
			source,
			target, 
			copyCache
		);

	persister.setPropertyValues( target, copiedValues, source.getEntityMode() );
}
 
Example #2
Source File: FromElementType.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void applyTreatAsDeclarations(Set<String> treatAsDeclarations) {
	if ( treatAsDeclarations != null && !treatAsDeclarations.isEmpty() ) {
		if ( this.treatAsDeclarations == null ) {
			this.treatAsDeclarations = new HashSet<String>();
		}

		for ( String treatAsSubclassName : treatAsDeclarations ) {
			try {
				EntityPersister subclassPersister = fromElement.getSessionFactoryHelper().requireClassPersister(
						treatAsSubclassName
				);
				this.treatAsDeclarations.add( subclassPersister.getEntityName() );
			}
			catch (SemanticException e) {
				throw new QueryException( "Unable to locate persister for subclass named in TREAT-AS : " + treatAsSubclassName );
			}
		}

		if ( joinSequence != null ) {
			joinSequence.applyTreatAsDeclarations( this.treatAsDeclarations );
		}
	}
}
 
Example #3
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Performs the process of loading an entity from the configured
 * underlying datasource.
 *
 * @param event The load event
 * @param persister The persister for the entity being requested for load
 *
 * @return The object loaded from the datasource, or null if not found.
 */
protected CompletionStage<Object> loadFromDatasource(
		final LoadEvent event,
		final EntityPersister persister) {

	CompletionStage<Object> entity =
			( (ReactiveEntityPersister) persister).reactiveLoad(
					event.getEntityId(),
					event.getInstanceToLoad(),
					event.getLockOptions(),
					event.getSession()
			);

	final StatisticsImplementor statistics = event.getSession().getFactory().getStatistics();
	if ( event.isAssociationFetch() && statistics.isStatisticsEnabled() ) {
		statistics.fetchEntity( event.getEntityClassName() );
	}

	return entity;
}
 
Example #4
Source File: ForeignKeys.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find all non-nullable references to entities that have not yet
 * been inserted in the database, where the foreign key
 * is a reference to an unsaved transient entity. .
 *
 * @param entityName - the entity name
 * @param entity - the entity instance
 * @param values - insertable properties of the object (including backrefs),
 * possibly with substitutions
 * @param isEarlyInsert - true if the entity needs to be executed as soon as possible
 * (e.g., to generate an ID)
 * @param session - the session
 *
 * @return the transient unsaved entity dependencies that are non-nullable,
 *         or null if there are none.
 */
public static NonNullableTransientDependencies findNonNullableTransientEntities(
		String entityName,
		Object entity,
		Object[] values,
		boolean isEarlyInsert,
		SharedSessionContractImplementor session) {
	final Nullifier nullifier = new Nullifier( entity, false, isEarlyInsert, session );
	final EntityPersister persister = session.getEntityPersister( entityName, entity );
	final String[] propertyNames = persister.getPropertyNames();
	final Type[] types = persister.getPropertyTypes();
	final boolean[] nullability = persister.getPropertyNullability();
	final NonNullableTransientDependencies nonNullableTransientEntities = new NonNullableTransientDependencies();
	for ( int i = 0; i < types.length; i++ ) {
		collectNonNullableTransientEntities(
				nullifier,
				values[i],
				propertyNames[i],
				types[i],
				nullability[i],
				session,
				nonNullableTransientEntities
		);
	}
	return nonNullableTransientEntities.isEmpty() ? null : nonNullableTransientEntities;
}
 
Example #5
Source File: DefaultReactiveDeleteEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * We encountered a delete request on a transient instance.
 * <p>
 * This is a deviation from historical Hibernate (pre-3.2) behavior to
 * align with the JPA spec, which states that transient entities can be
 * passed to remove operation in which case cascades still need to be
 * performed.
 *
 * @param session The session which is the source of the event
 * @param entity The entity being delete processed
 * @param cascadeDeleteEnabled Is cascading of deletes enabled
 * @param persister The entity persister
 * @param transientEntities A cache of already visited transient entities
 * (to avoid infinite recursion).
 */
protected CompletionStage<Void> deleteTransientEntity(
		EventSource session,
		Object entity,
		boolean cascadeDeleteEnabled,
		EntityPersister persister,
		IdentitySet transientEntities) {
	LOG.handlingTransientEntity();
	if ( transientEntities.contains( entity ) ) {
		LOG.trace( "Already handled transient entity; skipping" );
		return CompletionStages.nullFuture();
	}
	transientEntities.add( entity );
	return cascadeBeforeDelete( session, persister, entity, null, transientEntities )
			.thenCompose( v -> cascadeAfterDelete( session, persister, entity, transientEntities ) );
}
 
Example #6
Source File: AbstractSaveEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Handles the calls needed to perform pre-save cascades for the given entity.
 *
 * @param source The session from whcih the save event originated.
 * @param persister The entity's persister instance.
 * @param entity The entity to be saved.
 * @param anything Generally cascade-specific data
 */
protected void cascadeBeforeSave(
		EventSource source,
		EntityPersister persister,
		Object entity,
		Object anything) {

	// cascade-save to many-to-one BEFORE the parent is saved
	source.getPersistenceContext().incrementCascadeLevel();
	try {
		new Cascade( getCascadeAction(), Cascade.BEFORE_INSERT_AFTER_DELETE, source )
				.cascade( persister, entity, anything );
	}
	finally {
		source.getPersistenceContext().decrementCascadeLevel();
	}
}
 
Example #7
Source File: HibernateEventListener.java    From development with Apache License 2.0 6 votes vote down vote up
private void removeLocalization(EntityPersister persister, Object entity) {
    if (entity instanceof DomainObject<?>) {
        DomainObject<?> obj = (DomainObject<?>) entity;
        List<LocalizedObjectTypes> objType = obj.getLocalizedObjectTypes();
        if (objType.size() > 0) {
            long key = obj.getKey();
            final StatelessSession session = persister.getFactory()
                    .openStatelessSession();
            Transaction tx = session.beginTransaction();
            org.hibernate.Query query = session
                    .createQuery("DELETE FROM LocalizedResource WHERE objectKey = :objectKey AND objectType IN (:objectType)");
            query.setParameter("objectKey", Long.valueOf(key));
            query.setParameterList("objectType", objType);
            query.executeUpdate();
            tx.commit();
            session.close();
        }
    }
}
 
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: DefaultReactivePersistEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #10
Source File: MessageHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate an info message string relating to a particular entity,.
 *
 * @param persister The persister for the entity
 * @param id The entity id value
 * @param identifierType The entity identifier type mapping
 * @param factory The session factory
 * @return An info string, in the form [FooBar#1]
 */
public static String infoString(
		EntityPersister persister, 
		Object id, 
		Type identifierType,
		SessionFactoryImplementor factory) {
	StringBuilder s = new StringBuilder();
	s.append( '[' );
	if( persister == null ) {
		s.append( "<null EntityPersister>" );
	}
	else {
		s.append( persister.getEntityName() );
	}
	s.append( '#' );

	if ( id == null ) {
		s.append( "<null>" );
	}
	else {
		s.append( identifierType.toLoggableString( id, factory ) );
	}
	s.append( ']' );

	return s.toString();
}
 
Example #11
Source File: DefaultDeleteEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void cascadeBeforeDelete(
		EventSource session,
		EntityPersister persister,
		Object entity,
		EntityEntry entityEntry,
		Set transientEntities) throws HibernateException {

	CacheMode cacheMode = session.getCacheMode();
	session.setCacheMode( CacheMode.GET );
	session.getPersistenceContext().incrementCascadeLevel();
	try {
		// cascade-delete to collections BEFORE the collection owner is deleted
		new Cascade( CascadingAction.DELETE, Cascade.AFTER_INSERT_BEFORE_DELETE, session )
				.cascade( persister, entity, transientEntities );
	}
	finally {
		session.getPersistenceContext().decrementCascadeLevel();
		session.setCacheMode( cacheMode );
	}
}
 
Example #12
Source File: DefaultEvictEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void doEvict(
	final Object object, 
	final EntityKey key, 
	final EntityPersister persister,
	final EventSource session) throws HibernateException {

	if ( log.isTraceEnabled() ) {
		log.trace( "evicting " + MessageHelper.infoString(persister) );
	}

	// remove all collections for the entity from the session-level cache
	if ( persister.hasCollections() ) {
		new EvictVisitor( session ).process( object, persister );
	}

	new Cascade( CascadingAction.EVICT, Cascade.AFTER_EVICT, session )
			.cascade( persister, object );
}
 
Example #13
Source File: AliasResolutionContextImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private EntityReferenceAliases createCollectionElementAliases(
		CollectionPersister collectionPersister,
		String tableAlias,
		String elementQuerySpaceUid) {

	if ( !collectionPersister.getElementType().isEntityType() ) {
		return null;
	}
	else {
		final EntityType entityElementType = (EntityType) collectionPersister.getElementType();
		return generateEntityReferenceAliases(
				elementQuerySpaceUid,
				tableAlias,
				(EntityPersister) entityElementType.getAssociatedJoinable( sessionFactory() )
		);
	}
}
 
Example #14
Source File: EntityType.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected final Object getIdentifier(Object value, SharedSessionContractImplementor session) throws HibernateException {
	if ( isReferenceToPrimaryKey() || uniqueKeyPropertyName == null ) {
		return ForeignKeys.getEntityIdentifierIfNotUnsaved(
				getAssociatedEntityName(),
				value,
				session
		); //tolerates nulls
	}
	else if ( value == null ) {
		return null;
	}
	else {
		EntityPersister entityPersister = getAssociatedEntityPersister( session.getFactory() );
		Object propertyValue = entityPersister.getPropertyValue( value, uniqueKeyPropertyName );
		// We now have the value of the property-ref we reference.  However,
		// we need to dig a little deeper, as that property might also be
		// an entity type, in which case we need to resolve its identitifier
		Type type = entityPersister.getPropertyType( uniqueKeyPropertyName );
		if ( type.isEntityType() ) {
			propertyValue = ( (EntityType) type ).getIdentifier( propertyValue, session );
		}

		return propertyValue;
	}
}
 
Example #15
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean isFoundInParent(
		String property, 
		Object childEntity, 
		EntityPersister persister, 
		CollectionPersister collectionPersister,
		Object potentialParent
) {
	Object collection = persister.getPropertyValue( 
			potentialParent, 
			property, 
			session.getEntityMode() 
		);
	return collection!=null && Hibernate.isInitialized(collection) &&
			collectionPersister.getCollectionType()
					.contains(collection, childEntity, session);
}
 
Example #16
Source File: DefaultSaveOrUpdateEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Determine the id to use for updating.
 *
 * @param entity The entity.
 * @param persister The entity persister
 * @param requestedId The requested identifier
 * @param entityMode The entity mode.
 *
 * @return The id.
 *
 * @throws TransientObjectException If the entity is considered transient.
 */
protected Serializable getUpdateId(
		Object entity,
		EntityPersister persister,
		Serializable requestedId,
		EntityMode entityMode) {
	// use the id assigned to the instance
	Serializable id = persister.getIdentifier( entity, entityMode );
	if ( id == null ) {
		// assume this is a newly instantiated transient object
		// which should be saved rather than updated
		throw new TransientObjectException(
				"The given object has a null identifier: " +
						persister.getEntityName()
		);
	}
	else {
		return id;
	}

}
 
Example #17
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a proxy, initialize it and/or narrow it provided either
 * is necessary.
 *
 * @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
 * @param proxy The proxy to narrow
 *
 * @return The created/existing proxy
 */
private Object returnNarrowedProxy(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final PersistenceContext persistenceContext,
		final Object proxy) {
	if ( traceEnabled ) {
		LOG.trace( "Entity proxy found in session cache" );
	}
	LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
	if ( li.isUnwrap() ) {
		return li.getImplementation();
	}
	Object impl = null;
	if ( !options.isAllowProxyCreation() ) {
		impl = load( event, persister, keyToLoad, options );
		if ( impl == null ) {
			event.getSession()
					.getFactory()
					.getEntityNotFoundDelegate()
					.handleEntityNotFound( persister.getEntityName(), keyToLoad.getIdentifier() );
		}
	}
	return persistenceContext.narrowProxy( proxy, persister, keyToLoad, impl );
}
 
Example #18
Source File: BasicTableTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testNormalBoundary() {
	EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() );
	assertClassAssignability( TableGenerator.class, persister.getIdentifierGenerator().getClass() );
	TableGenerator generator = ( TableGenerator ) persister.getIdentifierGenerator();

	int count = 5;
	Entity[] entities = new Entity[count];
	Session s = openSession();
	s.beginTransaction();
	for ( int i = 0; i < count; i++ ) {
		entities[i] = new Entity( "" + ( i + 1 ) );
		s.save( entities[i] );
		long expectedId = i + 1;
		assertEquals( expectedId, entities[i].getId().longValue() );
		assertEquals( expectedId, generator.getTableAccessCount() );
		assertEquals( expectedId, generator.getOptimizer().getLastSourceValue() );
	}
	s.getTransaction().commit();

	s.beginTransaction();
	for ( int i = 0; i < count; i++ ) {
		assertEquals( i + 1, entities[i].getId().intValue() );
		s.delete( entities[i] );
	}
	s.getTransaction().commit();
	s.close();

}
 
Example #19
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
private CompletionStage<Void> checkIdClass(
		final EntityPersister persister,
		final LoadEvent event,
		final LoadEventListener.LoadType loadType,
		final Class<?> idClass) {
	// we may have the kooky jpa requirement of allowing find-by-id where
	// "id" is the "simple pk value" of a dependent objects parent.  This
	// is part of its generally goofy "derived identity" "feature"
	final IdentifierProperty identifierProperty = persister.getEntityMetamodel().getIdentifierProperty();
	if ( identifierProperty.isEmbedded() ) {
		final EmbeddedComponentType dependentIdType = (EmbeddedComponentType) identifierProperty.getType();
		if ( dependentIdType.getSubtypes().length == 1 ) {
			final Type singleSubType = dependentIdType.getSubtypes()[0];
			if ( singleSubType.isEntityType() ) {
				final EntityType dependentParentType = (EntityType) singleSubType;
				final SessionFactoryImplementor factory = event.getSession().getFactory();
				final Type dependentParentIdType = dependentParentType.getIdentifierOrUniqueKeyType( factory );
				if ( dependentParentIdType.getReturnedClass().isInstance( event.getEntityId() ) ) {
					// yep that's what we have...
					return loadByDerivedIdentitySimplePkValue( event, loadType, persister,
							dependentIdType, factory.getMetamodel().entityPersister( dependentParentType.getAssociatedEntityName() )
					);
				}
			}
		}
	}
	throw new TypeMismatchException(
			"Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass() );
}
 
Example #20
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Perfoms the load of an entity.
 *
 * @return The loaded entity.
 * @throws HibernateException
 */
protected Object load(
	final LoadEvent event, 
	final EntityPersister persister, 
	final EntityKey keyToLoad, 
	final LoadEventListener.LoadType options)
throws HibernateException {

	if ( event.getInstanceToLoad() != null ) {
		if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) {
			throw new PersistentObjectException(
					"attempted to load into an instance that was already associated with the session: " +
					MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() )
				);
		}
		persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession().getEntityMode() );
	}

	Object entity = doLoad(event, persister, keyToLoad, options);
	
	boolean isOptionalInstance = event.getInstanceToLoad() != null;
	
	if ( !options.isAllowNulls() || isOptionalInstance ) {
		if ( entity == null ) {
			event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
		}
	}

	if ( isOptionalInstance && entity != event.getInstanceToLoad() ) {
		throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
	}

	return entity;
}
 
Example #21
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Attempts to locate the entity in the session-level cache.
 * <p/>
 * If allowed to return nulls, then if the entity happens to be found in
 * the session cache, we check the entity type for proper handling
 * of entity hierarchies.
 * <p/>
 * If checkDeleted was set to true, then if the entity is found in the
 * session-level cache, it's current status within the session cache
 * is checked to see if it has previously been scheduled for deletion.
 *
 * @param event The load event
 * @param keyToLoad The EntityKey representing the entity to be loaded.
 * @param options The load options.
 * @return The entity from the session-level cache, or null.
 * @throws HibernateException Generally indicates problems applying a lock-mode.
 */
protected Object loadFromSessionCache(
		final LoadEvent event,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) throws HibernateException {
	
	SessionImplementor session = event.getSession();
	Object old = session.getEntityUsingInterceptor( keyToLoad );

	if ( old != null ) {
		// this object was already loaded
		EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
		if ( options.isCheckDeleted() ) {
			Status status = oldEntry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return REMOVED_ENTITY_MARKER;
			}
		}
		if ( options.isAllowNulls() ) {
			EntityPersister persister = event.getSession().getFactory().getEntityPersister( event.getEntityClassName() );
			if ( ! persister.isInstance( old, event.getSession().getEntityMode() ) ) {
				return INCONSISTENT_RTN_CLASS_MARKER;
			}
		}
		upgradeLock( old, oldEntry, event.getLockMode(), session );
	}

	return old;
}
 
Example #22
Source File: FromElementFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private FromElement createAndAddFromElement(
		String className,
		String classAlias,
		EntityPersister entityPersister,
		EntityType type,
		String tableAlias) {
	if ( !( entityPersister instanceof Joinable ) ) {
		throw new IllegalArgumentException( "EntityPersister " + entityPersister + " does not implement Joinable!" );
	}
	FromElement element = createFromElement( entityPersister );
	initializeAndAddFromElement( element, className, classAlias, entityPersister, type, tableAlias );
	return element;
}
 
Example #23
Source File: AbstractSaveEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void markInterceptorDirty(Object entity, EntityPersister persister, EventSource source) {
	if ( FieldInterceptionHelper.isInstrumented( entity ) ) {
		FieldInterceptor interceptor = FieldInterceptionHelper.injectFieldInterceptor(
				entity,
				persister.getEntityName(),
				null,
				source
		);
		interceptor.dirty();
	}
}
 
Example #24
Source File: NaturalIdXrefDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Handle removing cross reference entries for the given natural-id/pk combo
 *
 * @param persister The persister representing the entity type.
 * @param pk The primary key value
 * @param naturalIdValues The natural id value(s)
 * 
 * @return The cached values, if any.  May be different from incoming values.
 */
public Object[] removeNaturalIdCrossReference(EntityPersister persister, Serializable pk, Object[] naturalIdValues) {
	persister = locatePersisterForKey( persister );
	validateNaturalId( persister, naturalIdValues );

	final NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get( persister );
	Object[] sessionCachedNaturalIdValues = null;
	if ( entityNaturalIdResolutionCache != null ) {
		final CachedNaturalId cachedNaturalId = entityNaturalIdResolutionCache.pkToNaturalIdMap
				.remove( pk );
		if ( cachedNaturalId != null ) {
			entityNaturalIdResolutionCache.naturalIdToPkMap.remove( cachedNaturalId );
			sessionCachedNaturalIdValues = cachedNaturalId.getValues();
		}
	}

	if ( persister.hasNaturalIdCache() ) {
		final NaturalIdDataAccess naturalIdCacheAccessStrategy = persister
				.getNaturalIdCacheAccessStrategy();
		final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session() );
		naturalIdCacheAccessStrategy.evict( naturalIdCacheKey );

		if ( sessionCachedNaturalIdValues != null
				&& !Arrays.equals( sessionCachedNaturalIdValues, naturalIdValues ) ) {
			final Object sessionNaturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( sessionCachedNaturalIdValues, persister, session() );
			naturalIdCacheAccessStrategy.evict( sessionNaturalIdCacheKey );
		}
	}

	return sessionCachedNaturalIdValues;
}
 
Example #25
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public EntityPersister getEntityPersister(String entityName) throws MappingException {
	EntityPersister result = (EntityPersister) entityPersisters.get(entityName);
	if (result==null) {
		throw new MappingException( "Unknown entity: " + entityName );
	}
	return result;
}
 
Example #26
Source File: EntityInsertAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public EntityInsertAction(
        Serializable id,
        Object[] state,
        Object instance,
        Object version,
        EntityPersister persister,
        SessionImplementor session) throws HibernateException {
	super( session, id, instance, persister );
	this.state = state;
	this.version = version;
}
 
Example #27
Source File: DefaultUpdateEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If the user specified an id, assign it to the instance and use that, 
 * otherwise use the id already assigned to the instance
 */
protected Serializable getUpdateId(
		Object entity,
		EntityPersister persister,
		Serializable requestedId,
		SessionImplementor session) throws HibernateException {
	if ( requestedId == null ) {
		return super.getUpdateId( entity, persister, requestedId, session );
	}
	else {
		persister.setIdentifier( entity, requestedId, session );
		return requestedId;
	}
}
 
Example #28
Source File: DefaultResolveNaturalIdEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Coordinates the efforts to load a given entity. First, an attempt is
 * made to load the entity from the session-level cache. If not found there,
 * an attempt is made to locate it in second-level cache. Lastly, an
 * attempt is made to load it directly from the datasource.
 *
 * @param event The load event
 *
 * @return The loaded entity, or null.
 */
protected Serializable resolveNaturalId(final ResolveNaturalIdEvent event) {
	final EntityPersister persister = event.getEntityPersister();

	final boolean traceEnabled = LOG.isTraceEnabled();
	if ( traceEnabled ) {
		LOG.tracev(
				"Attempting to resolve: {0}#{1}",
				MessageHelper.infoString( persister ),
				event.getNaturalIdValues()
		);
	}

	Serializable entityId = resolveFromCache( event );
	if ( entityId != null ) {
		if ( traceEnabled ) {
			LOG.tracev(
					"Resolved object in cache: {0}#{1}",
					MessageHelper.infoString( persister ),
					event.getNaturalIdValues()
			);
		}
		return entityId;
	}

	if ( traceEnabled ) {
		LOG.tracev(
				"Object not resolved in any cache: {0}#{1}",
				MessageHelper.infoString( persister ),
				event.getNaturalIdValues()
		);
	}

	return loadFromDatasource( event );
}
 
Example #29
Source File: PostInsertEvent.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PostInsertEvent(
		Object entity, 
		Serializable id,
		Object[] state,
		EntityPersister persister,
		EventSource source
) {
	super(source);
	this.entity = entity;
	this.id = id;
	this.state = state;
	this.persister = persister;
}
 
Example #30
Source File: ForeignKeys.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is this instance, which we know is not persistent, actually transient?
 * <p/>
 * If <tt>assumed</tt> is non-null, don't hit the database to make the determination, instead assume that
 * value; the client code must be prepared to "recover" in the case that this assumed result is incorrect.
 *
 * @param entityName The name of the entity
 * @param entity The entity instance
 * @param assumed The assumed return value, if avoiding database hit is desired
 * @param session The session
 *
 * @return {@code true} if the given entity is transient (unsaved)
 */
public static boolean isTransient(String entityName, Object entity, Boolean assumed, SharedSessionContractImplementor session) {
	if ( entity == LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
		// an unfetched association can only point to
		// an entity that already exists in the db
		return false;
	}

	// let the interceptor inspect the instance to decide
	Boolean isUnsaved = session.getInterceptor().isTransient( entity );
	if ( isUnsaved != null ) {
		return isUnsaved;
	}

	// let the persister inspect the instance to decide
	final EntityPersister persister = session.getEntityPersister( entityName, entity );
	isUnsaved = persister.isTransient( entity, session );
	if ( isUnsaved != null ) {
		return isUnsaved;
	}

	// we use the assumed value, if there is one, to avoid hitting
	// the database
	if ( assumed != null ) {
		return assumed;
	}

	// hit the database, after checking the session cache for a snapshot
	final Object[] snapshot = session.getPersistenceContext().getDatabaseSnapshot(
			persister.getIdentifier( entity, session ),
			persister
	);
	return snapshot == null;

}