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

The following examples show how to use org.hibernate.persister.entity.EntityPersister#getPropertyValue() . 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: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object[] extractNaturalIdValues(Object entity, EntityPersister persister) {
	if ( entity == null ) {
		throw new AssertionFailure( "Entity from which to extract natural id value(s) cannot be null" );
	}
	if ( persister == null ) {
		throw new AssertionFailure( "Persister to use in extracting natural id value(s) cannot be null" );
	}

	final int[] naturalIdentifierProperties = persister.getNaturalIdentifierProperties();
	final Object[] naturalIdValues = new Object[naturalIdentifierProperties.length];

	for ( int i = 0; i < naturalIdentifierProperties.length; i++ ) {
		naturalIdValues[i] = persister.getPropertyValue( entity, naturalIdentifierProperties[i] );
	}

	return naturalIdValues;
}
 
Example 2
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 3
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 4
Source File: EntityType.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected final Object getIdentifier(Object value, SessionImplementor session) throws HibernateException {
	if ( isNotEmbedded(session) ) {
		return value;
	}

	if ( isReferenceToPrimaryKey() ) {
		return ForeignKeys.getEntityIdentifierIfNotUnsaved( getAssociatedEntityName(), value, session ); //tolerates nulls
	}
	else if ( value == null ) {
		return null;
	}
	else {
		EntityPersister entityPersister = session.getFactory().getEntityPersister( getAssociatedEntityName() );
		Object propertyValue = entityPersister.getPropertyValue( value, uniqueKeyPropertyName, session.getEntityMode() );
		// 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 5
Source File: CascadingActions.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void noCascade(
		EventSource session,
		Object parent,
		EntityPersister persister,
		Type propertyType,
		int propertyIndex) {
	if ( propertyType.isEntityType() ) {
		Object child = persister.getPropertyValue( parent, propertyIndex );
		String childEntityName = ((EntityType) propertyType).getAssociatedEntityName( session.getFactory() );

		if ( child != null
				&& !isInManagedState( child, session )
				&& !(child instanceof HibernateProxy) //a proxy cannot be transient and it breaks ForeignKeys.isTransient
				&& ForeignKeys.isTransient( childEntityName, child, null, session ) ) {
			String parentEntityName = persister.getEntityName();
			String propertyName = persister.getPropertyNames()[propertyIndex];
			throw new TransientPropertyValueException(
					"object references an unsaved transient instance - save the transient instance before flushing",
					childEntityName,
					parentEntityName,
					propertyName
			);

		}
	}
}
 
Example 6
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean isFoundInParent(
		String property,
		Object childEntity,
		EntityPersister persister,
		CollectionPersister collectionPersister,
		Object potentialParent) {
	final Object collection = persister.getPropertyValue( potentialParent, property );
	return collection != null
			&& Hibernate.isInitialized( collection )
			&& collectionPersister.getCollectionType().contains( collection, childEntity, session );
}
 
Example 7
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object getIndexInParent(
		String property,
		Object childEntity,
		EntityPersister persister,
		CollectionPersister collectionPersister,
		Object potentialParent){
	final Object collection = persister.getPropertyValue( potentialParent, property );
	if ( collection != null && Hibernate.isInitialized( collection ) ) {
		return collectionPersister.getCollectionType().indexOf( collection, childEntity );
	}
	else {
		return null;
	}
}
 
Example 8
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Object getIndexInParent(
		String property, 
		Object childEntity, 
		EntityPersister persister, 
		CollectionPersister collectionPersister,
		Object potentialParent
){	
	Object collection = persister.getPropertyValue( potentialParent, property, session.getEntityMode() );
	if ( collection!=null && Hibernate.isInitialized(collection) ) {
		return collectionPersister.getCollectionType().indexOf(collection, childEntity);
	}
	else {
		return null;
	}
}
 
Example 9
Source File: CollectionCacheInvalidator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void evictCache(Object entity, EntityPersister persister, EventSource session, Object[] oldState) {
	try {
		SessionFactoryImplementor factory = persister.getFactory();

		Set<String> collectionRoles = factory.getMetamodel().getCollectionRolesByEntityParticipant( persister.getEntityName() );
		if ( collectionRoles == null || collectionRoles.isEmpty() ) {
			return;
		}
		for ( String role : collectionRoles ) {
			final CollectionPersister collectionPersister = factory.getMetamodel().collectionPersister( role );
			if ( !collectionPersister.hasCache() ) {
				// ignore collection if no caching is used
				continue;
			}
			// this is the property this OneToMany relation is mapped by
			String mappedBy = collectionPersister.getMappedByProperty();
			if ( !collectionPersister.isManyToMany() &&
					mappedBy != null && !mappedBy.isEmpty() ) {
				int i = persister.getEntityMetamodel().getPropertyIndex( mappedBy );
				Serializable oldId = null;
				if ( oldState != null ) {
					// in case of updating an entity we perhaps have to decache 2 entity collections, this is the
					// old one
					oldId = getIdentifier( session, oldState[i] );
				}
				Object ref = persister.getPropertyValue( entity, i );
				Serializable id = getIdentifier( session, ref );

				// only evict if the related entity has changed
				if ( ( id != null && !id.equals( oldId ) ) || ( oldId != null && !oldId.equals( id ) ) ) {
					if ( id != null ) {
						evict( id, collectionPersister, session );
					}
					if ( oldId != null ) {
						evict( oldId, collectionPersister, session );
					}
				}
			}
			else {
				LOG.debug( "Evict CollectionRegion " + role );
				final SoftLock softLock = collectionPersister.getCacheAccessStrategy().lockRegion();
				session.getActionQueue().registerProcess( (success, session1) -> {
					collectionPersister.getCacheAccessStrategy().unlockRegion( softLock );
				} );
			}
		}
	}
	catch ( Exception e ) {
		if ( PROPAGATE_EXCEPTION ) {
			throw new IllegalStateException( e );
		}
		// don't let decaching influence other logic
		LOG.error( "", e );
	}
}