Java Code Examples for org.hibernate.type.Type#nullSafeSet()

The following examples show how to use org.hibernate.type.Type#nullSafeSet() . 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: QueryLoader.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private int bindFilterParameterValues(
		PreparedStatement st,
		QueryParameters queryParameters,
		int position,
		SessionImplementor session) throws SQLException {
	// todo : better to handle dynamic filters through implicit DynamicFilterParameterSpecification
	// see the discussion there in DynamicFilterParameterSpecification's javadocs as to why
	// it is currently not done that way.
	int filteredParamCount = queryParameters.getFilteredPositionalParameterTypes() == null
			? 0
			: queryParameters.getFilteredPositionalParameterTypes().length;
	int nonfilteredParamCount = queryParameters.getPositionalParameterTypes() == null
			? 0
			: queryParameters.getPositionalParameterTypes().length;
	int filterParamCount = filteredParamCount - nonfilteredParamCount;
	for ( int i = 0; i < filterParamCount; i++ ) {
		Type type = queryParameters.getFilteredPositionalParameterTypes()[i];
		Object value = queryParameters.getFilteredPositionalParameterValues()[i];
		type.nullSafeSet( st, value, position, session );
		position += type.getColumnSpan( getFactory() );
	}

	return position;
}
 
Example 2
Source File: AbstractCollectionPersister.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean exists(Serializable key, Object indexOrElement, Type indexOrElementType, String sql, SharedSessionContractImplementor session) {
	try {
		PreparedStatement st = session
				.getJdbcCoordinator()
				.getStatementPreparer()
				.prepareStatement( sql );
		try {
			getKeyType().nullSafeSet( st, key, 1, session );
			indexOrElementType.nullSafeSet( st, indexOrElement, keyColumnNames.length + 1, session );
			ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( st );
			try {
				return rs.next();
			}
			finally {
				session.getJdbcCoordinator().getResourceRegistry().release( rs, st );
			}
		}
		catch ( TransientObjectException e ) {
			return false;
		}
		finally {
			session.getJdbcCoordinator().getResourceRegistry().release( st );
			session.getJdbcCoordinator().afterStatementExecution();
		}
	}
	catch ( SQLException sqle ) {
		throw getSQLExceptionHelper().convert(
				sqle,
				"could not check row existence: " +
						MessageHelper.collectionInfoString( this, key, getFactory() ),
				sqlSelectSizeString
		);
	}
}
 
Example 3
Source File: AbstractCollectionPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean exists(Serializable key, Object indexOrElement, Type indexOrElementType, String sql, SessionImplementor session) {
	try {
		PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
		try {
			getKeyType().nullSafeSet(st, key, 1, session);
			indexOrElementType.nullSafeSet( st, indexOrElement, keyColumnNames.length + 1, session );
			ResultSet rs = st.executeQuery();
			try {
				return rs.next();
			}
			finally {
				rs.close();
			}
		}
		catch( TransientObjectException e ) {
			return false;
		}
		finally {
			session.getBatcher().closeStatement( st );
		}
	}
	catch (SQLException sqle) {
		throw JDBCExceptionHelper.convert(
				getFactory().getSQLExceptionConverter(),
				sqle,
				"could not check row existence: " + 
				MessageHelper.collectionInfoString( this, key, getFactory() ),
				sqlSelectSizeString
			);
	}
}
 
Example 4
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Serializable getIdByUniqueKey(Serializable key, String uniquePropertyName, SharedSessionContractImplementor session)
		throws HibernateException {
	if ( LOG.isTraceEnabled() ) {
		LOG.tracef(
				"resolving unique key [%s] to identifier for entity [%s]",
				key,
				getEntityName()
		);
	}

	int propertyIndex = getSubclassPropertyIndex( uniquePropertyName );
	if ( propertyIndex < 0 ) {
		throw new HibernateException(
				"Could not determine Type for property [" + uniquePropertyName + "] on entity [" + getEntityName() + "]"
		);
	}
	Type propertyType = getSubclassPropertyType( propertyIndex );

	try {
		PreparedStatement ps = session
				.getJdbcCoordinator()
				.getStatementPreparer()
				.prepareStatement( generateIdByUniqueKeySelectString( uniquePropertyName ) );
		try {
			propertyType.nullSafeSet( ps, key, 1, session );
			ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( ps );
			try {
				//if there is no resulting row, return null
				if ( !rs.next() ) {
					return null;
				}
				return (Serializable) getIdentifierType().nullSafeGet( rs, getIdentifierAliases(), session, null );
			}
			finally {
				session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( rs, ps );
			}
		}
		finally {
			session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( ps );
			session.getJdbcCoordinator().afterStatementExecution();
		}
	}
	catch (SQLException e) {
		throw session.getJdbcServices().getSqlExceptionHelper().convert(
				e,
				String.format(
						"could not resolve unique property [%s] to identifier for entity [%s]",
						uniquePropertyName,
						getEntityName()
				),
				getSQLSnapshotSelectString()
		);
	}

}
 
Example 5
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Serializable loadEntityIdByNaturalId(
		Object[] naturalIdValues,
		LockOptions lockOptions,
		SharedSessionContractImplementor session) {
	if ( LOG.isTraceEnabled() ) {
		LOG.tracef(
				"Resolving natural-id [%s] to id : %s ",
				naturalIdValues,
				MessageHelper.infoString( this )
		);
	}

	final boolean[] valueNullness = determineValueNullness( naturalIdValues );
	final String sqlEntityIdByNaturalIdString = determinePkByNaturalIdQuery( valueNullness );

	try {
		PreparedStatement ps = session
				.getJdbcCoordinator()
				.getStatementPreparer()
				.prepareStatement( sqlEntityIdByNaturalIdString );
		try {
			int positions = 1;
			int loop = 0;
			for ( int idPosition : getNaturalIdentifierProperties() ) {
				final Object naturalIdValue = naturalIdValues[loop++];
				if ( naturalIdValue != null ) {
					final Type type = getPropertyTypes()[idPosition];
					type.nullSafeSet( ps, naturalIdValue, positions, session );
					positions += type.getColumnSpan( session.getFactory() );
				}
			}
			ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( ps );
			try {
				// if there is no resulting row, return null
				if ( !rs.next() ) {
					return null;
				}

				final Object hydratedId = getIdentifierType().hydrate( rs, getIdentifierAliases(), session, null );
				return (Serializable) getIdentifierType().resolve( hydratedId, session, null );
			}
			finally {
				session.getJdbcCoordinator().getResourceRegistry().release( rs, ps );
			}
		}
		finally {
			session.getJdbcCoordinator().getResourceRegistry().release( ps );
			session.getJdbcCoordinator().afterStatementExecution();
		}
	}
	catch (SQLException e) {
		throw getFactory().getSQLExceptionHelper().convert(
				e,
				String.format(
						"could not resolve natural-id [%s] to id : %s",
						naturalIdValues,
						MessageHelper.infoString( this )
				),
				sqlEntityIdByNaturalIdString
		);
	}
}
 
Example 6
Source File: PositionalParameterSpecification.java    From cacheonix-core with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Bind the appropriate value into the given statement at the specified position.
 *
 * @param statement The statement into which the value should be bound.
 * @param qp The defined values for the current query execution.
 * @param session The session against which the current execution is occuring.
 * @param position The position from which to start binding value(s).
 *
 * @return The number of sql bind positions "eaten" by this bind operation.
 */
public int bind(PreparedStatement statement, QueryParameters qp, SessionImplementor session, int position) throws SQLException {
	Type type = qp.getPositionalParameterTypes()[hqlPosition];
	Object value = qp.getPositionalParameterValues()[hqlPosition];

	type.nullSafeSet( statement, value, position, session );
	return type.getColumnSpan( session.getFactory() );
}