org.hibernate.engine.SessionImplementor Java Examples

The following examples show how to use org.hibernate.engine.SessionImplementor. 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: AnyType.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Object replace(
		Object original, 
		Object target,
		SessionImplementor session, 
		Object owner, 
		Map copyCache)
throws HibernateException {
	if (original==null) {
		return null;
	}
	else {
		String entityName = session.bestGuessEntityName(original);
		Serializable id = ForeignKeys.getEntityIdentifierIfNotUnsaved( 
				entityName, 
				original, 
				session 
			);
		return session.internalLoad( 
				entityName, 
				id, 
				false, 
				false
			);
	}
}
 
Example #2
Source File: ArrayType.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Object replaceElements(
	Object original,
	Object target,
	Object owner, 
	Map copyCache, 
	SessionImplementor session)
throws HibernateException {
	
	int length = Array.getLength(original);
	if ( length!=Array.getLength(target) ) {
		//note: this affects the return value!
		target=instantiateResult(original);
	}
	
	Type elemType = getElementType( session.getFactory() );
	for ( int i=0; i<length; i++ ) {
		Array.set( target, i, elemType.replace( Array.get(original, i), null, session, owner, copyCache ) );
	}
	
	return target;

}
 
Example #3
Source File: TypeFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Apply the {@link Type#replace} operation across a series of values.
 *
 * @param original The source of the state
 * @param target The target into which to replace the source values.
 * @param types The value types
 * @param session The orginating session
 * @param owner The entity "owning" the values
 * @param copyCache A map representing a cache of already replaced state
 * @return The replaced state
 */
public static Object[] replace(
		final Object[] original,
		final Object[] target,
		final Type[] types,
		final SessionImplementor session,
		final Object owner,
		final Map copyCache) {
	Object[] copied = new Object[original.length];
	for ( int i = 0; i < types.length; i++ ) {
		if ( original[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
			|| original[i] == BackrefPropertyAccessor.UNKNOWN ) {
			copied[i] = target[i];
		}
		else {
			copied[i] = types[i].replace( original[i], target[i], session, owner, copyCache );
		}
	}
	return copied;
}
 
Example #4
Source File: Loader.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void putResultInQueryCache(
		final SessionImplementor session, 
		final QueryParameters queryParameters, 
		final Type[] resultTypes,
		final QueryCache queryCache, 
		final QueryKey key, 
		final List result) {
	
	if ( session.getCacheMode().isPutEnabled() ) {
		boolean put = queryCache.put(
				key, 
				resultTypes, 
				result, 
				queryParameters.isNaturalKeyLookup(), 
				session 
		);
		if ( put && factory.getStatistics().isStatisticsEnabled() ) {
			factory.getStatisticsImplementor()
					.queryCachePut( getQueryIdentifier(), queryCache.getRegionName() );
		}
	}
}
 
Example #5
Source File: CriteriaLoader.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected Object getResultColumnOrRow(Object[] row, ResultTransformer transformer, ResultSet rs, SessionImplementor session)
throws SQLException, HibernateException {
	final Object[] result;
	final String[] aliases;
	if ( translator.hasProjection() ) {
		Type[] types = translator.getProjectedTypes();
		result = new Object[types.length];
		String[] columnAliases = translator.getProjectedColumnAliases();
		for ( int i=0; i<result.length; i++ ) {
			result[i] = types[i].nullSafeGet(rs, columnAliases[i], session, null);
		}
		aliases = translator.getProjectedAliases();
	}
	else {
		result = row;
		aliases = userAliases;
	}
	return translator.getRootCriteria().getResultTransformer()
			.transformTuple(result, aliases);
}
 
Example #6
Source File: PersistentOwnedSet.java    From webdsl with Apache License 2.0 6 votes vote down vote up
@Override
public boolean endRead() {
	//afterInitialize(); // Needed for DelayedOperations
	boolean result = super.endRead();
	((utils.OwnedSet)set).setDoEvents(true); // We should resume updating the inverse, because initialization is complete

	if(this.restoreFilter != null) {
		// Restore the filter that was enabled before enabling the filter hint
		SessionImplementor session = getSession();
		org.hibernate.engine.LoadQueryInfluencers lqi = session.getLoadQueryInfluencers();
		org.hibernate.impl.FilterImpl oldFilter = this.getAffectingFilter();
		if(oldFilter != null) lqi.disableFilter(oldFilter.getName());
		utils.QueryOptimization.restoreFilter(lqi, this.restoreFilter);
		this.restoreFilter = null;
	}

	return result;
}
 
Example #7
Source File: MultipleHiLoPerTableGenerator.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public synchronized Serializable generate(SessionImplementor session, Object obj)
	throws HibernateException {
	if (maxLo < 1) {
		//keep the behavior consistent even for boundary usages
		int val = ( (Integer) doWorkInNewTransaction(session) ).intValue();
		if (val == 0) val = ( (Integer) doWorkInNewTransaction(session) ).intValue();
		return IdentifierGeneratorFactory.createNumber( val, returnClass );
	}
	if (lo>maxLo) {
		int hival = ( (Integer) doWorkInNewTransaction(session) ).intValue();
		lo = (hival == 0) ? 1 : 0;
		hi = hival * (maxLo+1);
		log.debug("new hi value: " + hival);
	}
	return IdentifierGeneratorFactory.createNumber( hi + lo++, returnClass );
}
 
Example #8
Source File: AbstractType.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Object replace(
		Object original, 
		Object target, 
		SessionImplementor session, 
		Object owner, 
		Map copyCache, 
		ForeignKeyDirection foreignKeyDirection) 
throws HibernateException {
	boolean include;
	if ( isAssociationType() ) {
		AssociationType atype = (AssociationType) this;
		include = atype.getForeignKeyDirection()==foreignKeyDirection;
	}
	else {
		include = ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT==foreignKeyDirection;
	}
	return include ? replace(original, target, session, owner, copyCache) : target;
}
 
Example #9
Source File: PersistentOwnedSet.java    From webdsl with Apache License 2.0 6 votes vote down vote up
protected FilterImpl getAffectingFilter(utils.AbstractOwnedSetType type) {
	SessionImplementor session = getSession();
	FilterImpl filter = null;
	LoadQueryInfluencers lqi = session.getLoadQueryInfluencers();
	if(lqi != null) {
		java.util.Map filters = lqi.getEnabledFilters();
		for(Object entry : filters.entrySet()) {
			if(!(entry instanceof java.util.Map.Entry)) continue;
			Object key = ((java.util.Map.Entry)entry).getKey();
			Object value = ((java.util.Map.Entry)entry).getValue();
			if(key != null && value != null && value instanceof org.hibernate.impl.FilterImpl && type.isAffectedBy(key.toString())) {
				if(filter == null) {
					filter = (org.hibernate.impl.FilterImpl) value;
				} else {
					throw new java.lang.UnsupportedOperationException("Filters '" + filter.getName() + "' and '" + key.toString() + "' both filter the same collection role" + (getRole() == null ? "." : " (" + getRole() + ")."));
				}
			}
		}
	}
	return filter;
}
 
Example #10
Source File: IdTransferringMergeEventListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Hibernate 3.1 implementation of ID transferral.
 */
@Override
protected void entityIsTransient(MergeEvent event, Map copyCache) {
	super.entityIsTransient(event, copyCache);
	SessionImplementor session = event.getSession();
	EntityPersister persister = session.getEntityPersister(event.getEntityName(), event.getEntity());
	// Extract id from merged copy (which is currently registered with Session).
	Serializable id = persister.getIdentifier(event.getResult(), session.getEntityMode());
	// Set id on original object (which remains detached).
	persister.setIdentifier(event.getOriginal(), id, session.getEntityMode());
}
 
Example #11
Source File: AbstractEntityPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Object initializeLazyPropertiesFromCache(
		final String fieldName,
		final Object entity,
		final SessionImplementor session,
		final EntityEntry entry,
		final CacheEntry cacheEntry
) {

	log.trace("initializing lazy properties from second-level cache");

	Object result = null;
	Serializable[] disassembledValues = cacheEntry.getDisassembledState();
	final Object[] snapshot = entry.getLoadedState();
	for ( int j = 0; j < lazyPropertyNames.length; j++ ) {
		final Object propValue = lazyPropertyTypes[j].assemble(
				disassembledValues[ lazyPropertyNumbers[j] ],
				session,
				entity
			);
		if ( initializeLazyProperty( fieldName, entity, session, snapshot, j, propValue ) ) {
			result = propValue;
		}
	}

	log.trace( "done initializing lazy properties" );

	return result;
}
 
Example #12
Source File: NullableType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public final Object nullSafeGet(
		ResultSet rs,
		String[] names,
		SessionImplementor session,
		Object owner) throws HibernateException, SQLException {
	return nullSafeGet(rs, names[0]);
}
 
Example #13
Source File: ScrollableResultsImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ScrollableResultsImpl(
        ResultSet rs,
        PreparedStatement ps,
        SessionImplementor sess,
        Loader loader,
        QueryParameters queryParameters,
        Type[] types, HolderInstantiator holderInstantiator) throws MappingException {
	super( rs, ps, sess, loader, queryParameters, types, holderInstantiator );
}
 
Example #14
Source File: IncrementGenerator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void getNext( SessionImplementor session ) {

		log.debug("fetching initial value: " + sql);
		
		try {
			PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
			try {
				ResultSet rs = st.executeQuery();
				try {
					if ( rs.next() ) {
						next = rs.getLong(1) + 1;
						if ( rs.wasNull() ) next = 1;
					}
					else {
						next = 1;
					}
					sql=null;
					log.debug("first free id: " + next);
				}
				finally {
					rs.close();
				}
			}
			finally {
				session.getBatcher().closeStatement(st);
			}
			
		}
		catch (SQLException sqle) {
			throw JDBCExceptionHelper.convert(
					session.getFactory().getSQLExceptionConverter(),
					sqle,
					"could not fetch initial value for increment generator",
					sql
				);
		}
	}
 
Example #15
Source File: Loadable.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Retrieve property values from one row of a result set
 */
public Object[] hydrate(
		ResultSet rs,
		Serializable id,
		Object object,
		Loadable rootLoadable,
		String[][] suffixedPropertyColumns,
		boolean allProperties, 
		SessionImplementor session)
throws SQLException, HibernateException;
 
Example #16
Source File: EntityType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Object replace(
		Object original,
		Object target,
		SessionImplementor session,
		Object owner,
		Map copyCache) throws HibernateException {
	if ( original == null ) {
		return null;
	}
	Object cached = copyCache.get(original);
	if ( cached != null ) {
		return cached;
	}
	else {
		if ( original == target ) {
			return target;
		}
		Object id = getIdentifier( original, session );
		if ( id == null ) {
			throw new AssertionFailure("cannot copy a reference to an object with a null id");
		}
		id = getIdentifierOrUniqueKeyType( session.getFactory() )
				.replace(id, null, session, owner, copyCache);
		return resolve( id, session, owner );
	}
}
 
Example #17
Source File: QueryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public QueryImpl(
		String queryString,
        FlushMode flushMode,
        SessionImplementor session,
        ParameterMetadata parameterMetadata) {
	super( queryString, flushMode, session, parameterMetadata );
}
 
Example #18
Source File: DoubleStringType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object assemble(
	Serializable cached,
	SessionImplementor session,
	Object owner) {

	return deepCopy(cached);
}
 
Example #19
Source File: CompositeCustomType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void nullSafeSet(
	PreparedStatement st,
	Object value,
	int index,
	boolean[] settable, 
	SessionImplementor session)
	throws HibernateException, SQLException {

	userType.nullSafeSet(st, value, index, session);

}
 
Example #20
Source File: AbstractOwnedListType.java    From webdsl with Apache License 2.0 5 votes vote down vote up
@Override
public Object replaceElements(Object original, Object target,
		CollectionPersister persister, Object owner, Map copyCache,
		SessionImplementor session) throws HibernateException {
	java.util.List result = (java.util.List) target;
	result.clear();
	result.addAll((java.util.List) original);
	return result;
}
 
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: AbstractCollectionPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Write the element to a JDBC <tt>PreparedStatement</tt>
 */
protected int writeElementToWhere(PreparedStatement st, Object elt, int i, SessionImplementor session)
		throws HibernateException, SQLException {
	if (elementIsPureFormula) {
		throw new AssertionFailure("cannot use a formula-based element in the where condition");
	}
	getElementType().nullSafeSet(st, elt, i, elementColumnIsInPrimaryKey, session);
	return i + elementColumnAliases.length;

}
 
Example #23
Source File: CompositeCustomType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object assemble(
	Serializable cached,
	SessionImplementor session,
	Object owner)
	throws HibernateException {

	return userType.assemble(cached, session, owner);
}
 
Example #24
Source File: MonetoryAmountUserType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void nullSafeSet(PreparedStatement st, Object value, int index,
		SessionImplementor session) throws HibernateException, SQLException {
	MonetoryAmount ma = (MonetoryAmount) value;
	BigDecimal amt = ma == null ? null : ma.getAmount();
	Currency cur = ma == null ? null : ma.getCurrency();
	Hibernate.BIG_DECIMAL.nullSafeSet(st, amt, index);
	Hibernate.CURRENCY.nullSafeSet(st, cur, index+1);
}
 
Example #25
Source File: MutableType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object replace(
	Object original,
	Object target,
	SessionImplementor session,
	Object owner, 
	Map copyCache)
throws HibernateException {
	if ( isEqual( original, target, session.getEntityMode() ) ) return original;
	return deepCopy( original, session.getEntityMode(), session.getFactory() );
}
 
Example #26
Source File: AbstractEntityPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Marshall the fields of a persistent instance to a prepared statement
 */
protected int dehydrate(
		final Serializable id,
        final Object[] fields,
        final Object rowId,
        final boolean[] includeProperty,
        final boolean[][] includeColumns,
        final int j,
        final PreparedStatement ps,
        final SessionImplementor session,
        int index) throws SQLException, HibernateException {

	if ( log.isTraceEnabled() ) {
		log.trace( "Dehydrating entity: " + MessageHelper.infoString( this, id, getFactory() ) );
	}

	for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
		if ( includeProperty[i] && isPropertyOfTable( i, j ) ) {
			getPropertyTypes()[i].nullSafeSet( ps, fields[i], index, includeColumns[i], session );
			//index += getPropertyColumnSpan( i );
			index += ArrayHelper.countTrue( includeColumns[i] ); //TODO:  this is kinda slow...
		}
	}

	if ( rowId != null ) {
		ps.setObject( index, rowId );
		index += 1;
	}
	else if ( id != null ) {
		getIdentifierType().nullSafeSet( ps, id, index, session );
		index += getIdentifierColumnSpan();
	}

	return index;

}
 
Example #27
Source File: AbstractEntityTuplizer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object[] getPropertyValuesToInsert(Object entity, Map mergeMap, SessionImplementor session) 
throws HibernateException {
	final int span = entityMetamodel.getPropertySpan();
	final Object[] result = new Object[span];

	for ( int j = 0; j < span; j++ ) {
		result[j] = getters[j].getForInsert( entity, mergeMap, session );
	}
	return result;
}
 
Example #28
Source File: AbstractLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Return the underlying persistent object in the given <tt>Session</tt>, or null,
 * do not initialize the proxy
 */
public final Object getImplementation(SessionImplementor s) throws HibernateException {
	final EntityKey entityKey = new EntityKey(
			getIdentifier(),
			s.getFactory().getEntityPersister( getEntityName() ),
			s.getEntityMode()
		);
	return s.getPersistenceContext().getEntity( entityKey );
}
 
Example #29
Source File: MonetoryAmountUserType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
		throws HibernateException, SQLException {
	BigDecimal amt = (BigDecimal) Hibernate.BIG_DECIMAL.nullSafeGet( rs, names[0] );
	Currency cur = (Currency) Hibernate.CURRENCY.nullSafeGet( rs, names[1] );
	if (amt==null) return null;
	return new MonetoryAmount(amt, cur);
}
 
Example #30
Source File: ImmutableType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object replace(
	Object original,
	Object target,
	SessionImplementor session,
	Object owner, 
	Map copyCache)
throws HibernateException {
	return original;
}