Java Code Examples for org.hibernate.engine.spi.SessionImplementor.getEntityPersister()

The following are Jave code examples for showing how to use getEntityPersister() of the org.hibernate.engine.spi.SessionImplementor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: ForeignKeys.java   View Source Code Vote up 6 votes
/**
 * 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,
		SessionImplementor 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 2
Project: lams   File: ResultSetProcessorHelper.java   View Source Code Vote up 5 votes
public EntityKey interpretEntityKey(
		SessionImplementor session,
		String optionalEntityName,
		Serializable optionalId,
		Object optionalObject) {
	if ( optionalEntityName != null ) {
		final EntityPersister entityPersister;
		if ( optionalObject != null ) {
			entityPersister = session.getEntityPersister( optionalEntityName, optionalObject );
		}
		else {
			entityPersister = session.getFactory().getEntityPersister( optionalEntityName );
		}
		if ( entityPersister.isInstance( optionalId ) &&
				!entityPersister.getEntityMetamodel().getIdentifierProperty().isVirtual() &&
				entityPersister.getEntityMetamodel().getIdentifierProperty().isEmbedded() ) {
			// non-encapsulated composite identifier
			final Serializable identifierState = ((CompositeType) entityPersister.getIdentifierType()).getPropertyValues(
					optionalId,
					session
			);
			return session.generateEntityKey( identifierState, entityPersister );
		}
		else {
			return session.generateEntityKey( optionalId, entityPersister );
		}
	}
	else {
		return null;
	}
}
 
Example 3
Project: lams   File: DiscriminatorType.java   View Source Code Vote up 5 votes
public Object nullSafeGet(
		ResultSet rs,
		String name,
		SessionImplementor session,
		Object owner) throws HibernateException, SQLException {
	final Object discriminatorValue = underlyingType.nullSafeGet( rs, name, session, owner );
	final String entityName = persister.getSubclassForDiscriminatorValue( discriminatorValue );
	if ( entityName == null ) {
		throw new HibernateException( "Unable to resolve discriminator value [" + discriminatorValue + "] to entity name" );
	}
	final EntityPersister entityPersister = session.getEntityPersister( entityName, null );
       return ( EntityMode.POJO == entityPersister.getEntityMode() ) ? entityPersister.getMappedClass() : entityName;
}
 
Example 4
Project: lams   File: ForeignKeys.java   View Source Code Vote up 5 votes
/**
 * 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, SessionImplementor 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;

}
 
Example 5
Project: lams   File: DefaultLockEventListener.java   View Source Code Vote up 5 votes
/**
 * Handle the given lock event.
 *
 * @param event The lock event to be handled.
 * @throws HibernateException
 */
public void onLock(LockEvent event) throws HibernateException {

	if ( event.getObject() == null ) {
		throw new NullPointerException( "attempted to lock null" );
	}

	if ( event.getLockMode() == LockMode.WRITE ) {
		throw new HibernateException( "Invalid lock mode for lock()" );
	}

	if ( event.getLockMode() == LockMode.UPGRADE_SKIPLOCKED ) {
		LOG.explicitSkipLockedLockCombo();
	}

	SessionImplementor source = event.getSession();
	
	Object entity = source.getPersistenceContext().unproxyAndReassociate( event.getObject() );
	//TODO: if object was an uninitialized proxy, this is inefficient,
	//      resulting in two SQL selects
	
	EntityEntry entry = source.getPersistenceContext().getEntry(entity);
	if (entry==null) {
		final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
		final Serializable id = persister.getIdentifier( entity, source );
		if ( !ForeignKeys.isNotTransient( event.getEntityName(), entity, Boolean.FALSE, source ) ) {
			throw new TransientObjectException(
					"cannot lock an unsaved transient instance: " +
					persister.getEntityName()
			);
		}

		entry = reassociate(event, entity, id, persister);
		cascadeOnLock(event, persister, entity);
	}

	upgradeLock( entity, entry, event.getLockOptions(), event.getSession() );
}