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

The following are Jave code examples for showing how to use nullSafeSet() of the org.hibernate.type.Type 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: AbstractCollectionPersister.java   View Source Code Vote up 5 votes
private boolean exists(Serializable key, Object indexOrElement, Type indexOrElementType, String sql, SessionImplementor session) {
	try {
		PreparedStatement st = session.getTransactionCoordinator()
				.getJdbcCoordinator()
				.getStatementPreparer()
				.prepareStatement( sql );
		try {
			getKeyType().nullSafeSet( st, key, 1, session );
			indexOrElementType.nullSafeSet( st, indexOrElement, keyColumnNames.length + 1, session );
			ResultSet rs = session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().extract( st );
			try {
				return rs.next();
			}
			finally {
				session.getTransactionCoordinator().getJdbcCoordinator().release( rs, st );
			}
		}
		catch ( TransientObjectException e ) {
			return false;
		}
		finally {
			session.getTransactionCoordinator().getJdbcCoordinator().release( st );
		}
	}
	catch ( SQLException sqle ) {
		throw getSQLExceptionHelper().convert(
				sqle,
				"could not check row existence: " +
						MessageHelper.collectionInfoString( this, key, getFactory() ),
				sqlSelectSizeString
		);
	}
}
 
Example 2
Project: lams   File: AbstractEntityPersister.java   View Source Code Vote up 4 votes
@Override
public Serializable getIdByUniqueKey(Serializable key, String uniquePropertyName, SessionImplementor 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.getTransactionCoordinator()
				.getJdbcCoordinator()
				.getStatementPreparer()
				.prepareStatement( generateIdByUniqueKeySelectString( uniquePropertyName ) );
		try {
			propertyType.nullSafeSet( ps, key, 1, session );
			ResultSet rs = session.getTransactionCoordinator().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.getTransactionCoordinator().getJdbcCoordinator().release( rs, ps );
			}
		}
		finally {
			session.getTransactionCoordinator().getJdbcCoordinator().release( ps );
		}
	}
	catch ( SQLException e ) {
		throw getFactory().getSQLExceptionHelper().convert(
				e,
				String.format(
						"could not resolve unique property [%s] to identifier for entity [%s]",
						uniquePropertyName,
						getEntityName()
				),
				getSQLSnapshotSelectString()
		);
	}

}
 
Example 3
Project: lams   File: AbstractEntityPersister.java   View Source Code Vote up 4 votes
@Override
public Serializable loadEntityIdByNaturalId(
		Object[] naturalIdValues,
		LockOptions lockOptions,
		SessionImplementor 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.getTransactionCoordinator()
				.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.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().extract( ps );
			try {
				// if there is no resulting row, return null
				if ( !rs.next() ) {
					return null;
				}

				return (Serializable) getIdentifierType().hydrate( rs, getIdentifierAliases(), session, null );
			}
			finally {
				session.getTransactionCoordinator().getJdbcCoordinator().release( rs, ps );
			}
		}
		finally {
			session.getTransactionCoordinator().getJdbcCoordinator().release( ps );
		}
	}
	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 4
Project: lams   File: PositionalParameterSpecification.java   View Source Code Vote up 3 votes
/**
 * 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.
 */
@Override
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() );
}