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

The following are Jave code examples for showing how to use getTenantIdentifier() 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: NaturalIdCacheKey.java   View Source Code Vote up 5 votes
/**
 * Construct a new key for a caching natural identifier resolutions into the second level cache.
 * Note that an entity name should always be the root entity name, not a subclass entity name.
 *
 * @param naturalIdValues The naturalIdValues associated with the cached data
 * @param persister The persister for the entity
 * @param session The originating session
 */
public NaturalIdCacheKey(
		final Object[] naturalIdValues,
		final EntityPersister persister,
		final SessionImplementor session) {

	this.entityName = persister.getRootEntityName();
	this.tenantId = session.getTenantIdentifier();

	this.naturalIdValues = new Serializable[naturalIdValues.length];

	final SessionFactoryImplementor factory = session.getFactory();
	final int[] naturalIdPropertyIndexes = persister.getNaturalIdentifierProperties();
	final Type[] propertyTypes = persister.getPropertyTypes();

	final int prime = 31;
	int result = 1;
	result = prime * result + ( ( this.entityName == null ) ? 0 : this.entityName.hashCode() );
	result = prime * result + ( ( this.tenantId == null ) ? 0 : this.tenantId.hashCode() );
	for ( int i = 0; i < naturalIdValues.length; i++ ) {
		final int naturalIdPropertyIndex = naturalIdPropertyIndexes[i];
		final Type type = propertyTypes[naturalIdPropertyIndex];
		final Object value = naturalIdValues[i];

		result = prime * result + (value != null ? type.getHashCode( value, factory ) : 0);

		// The natural id may not be fully resolved in some situations.  See HHH-7513 for one of them
		// (re-attaching a mutable natural id uses a database snapshot and hydration does not resolve associations).
		// TODO: The snapshot should probably be revisited at some point.  Consider semi-resolving, hydrating, etc.
		if (type instanceof EntityType && type.getSemiResolvedType( factory ).getReturnedClass().isInstance( value )) {
			this.naturalIdValues[i] = (Serializable) value;
		}
		else {
			this.naturalIdValues[i] = type.disassemble( value, session, null );
		}
	}

	this.hashCode = result;
	initTransients();
}
 
Example 2
Project: lams   File: TableStructure.java   View Source Code Vote up 4 votes
@Override
public AccessCallback buildCallback(final SessionImplementor session) {
	final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry()
			.getService( JdbcServices.class )
			.getSqlStatementLogger();
	final SessionEventListenerManager statsCollector = session.getEventListenerManager();

	return new AccessCallback() {
		@Override
		public IntegralDataTypeHolder getNextValue() {
			return session.getTransactionCoordinator().getTransaction().createIsolationDelegate().delegateWork(
					new AbstractReturningWork<IntegralDataTypeHolder>() {
						@Override
						public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
							final IntegralDataTypeHolder value = makeValue();
							int rows;
							do {
								final PreparedStatement selectStatement = prepareStatement( connection, selectQuery, statementLogger, statsCollector );
								try {
									final ResultSet selectRS = executeQuery( selectStatement, statsCollector );
									if ( !selectRS.next() ) {
										final String err = "could not read a hi value - you need to populate the table: " + tableName;
										LOG.error( err );
										throw new IdentifierGenerationException( err );
									}
									value.initialize( selectRS, 1 );
									selectRS.close();
								}
								catch (SQLException sqle) {
									LOG.error( "could not read a hi value", sqle );
									throw sqle;
								}
								finally {
									selectStatement.close();
								}


								final PreparedStatement updatePS = prepareStatement( connection, updateQuery, statementLogger, statsCollector );
								try {
									final int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
									final IntegralDataTypeHolder updateValue = value.copy().add( increment );
									updateValue.bind( updatePS, 1 );
									value.bind( updatePS, 2 );
									rows = executeUpdate( updatePS, statsCollector );
								}
								catch (SQLException e) {
									LOG.unableToUpdateQueryHiValue( tableName, e );
									throw e;
								}
								finally {
									updatePS.close();
								}
							} while ( rows == 0 );

							accessCounter++;

							return value;
						}
					},
					true
			);
		}

		@Override
		public String getTenantIdentifier() {
			return session.getTenantIdentifier();
		}
	};
}
 
Example 3
Project: lams   File: SequenceStructure.java   View Source Code Vote up 4 votes
@Override
public AccessCallback buildCallback(final SessionImplementor session) {
	return new AccessCallback() {
		@Override
		public IntegralDataTypeHolder getNextValue() {
			accessCounter++;
			try {
				final PreparedStatement st = session.getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer().prepareStatement( sql );
				try {
					final ResultSet rs = session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().extract( st );
					try {
						rs.next();
						final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder( numberType );
						value.initialize( rs, 1 );
						if ( LOG.isDebugEnabled() ) {
							LOG.debugf( "Sequence value obtained: %s", value.makeValue() );
						}
						return value;
					}
					finally {
						try {
							session.getTransactionCoordinator().getJdbcCoordinator().release( rs, st );
						}
						catch( Throwable ignore ) {
							// intentionally empty
						}
					}
				}
				finally {
					session.getTransactionCoordinator().getJdbcCoordinator().release( st );
				}

			}
			catch ( SQLException sqle) {
				throw session.getFactory().getSQLExceptionHelper().convert(
						sqle,
						"could not get next sequence value",
						sql
				);
			}
		}

		@Override
		public String getTenantIdentifier() {
			return session.getTenantIdentifier();
		}
	};
}
 
Example 4
Project: lams   File: QueryKey.java   View Source Code Vote up 4 votes
/**
 * Generates a QueryKey.
 *
 * @param queryString The sql query string.
 * @param queryParameters The query parameters
 * @param filterKeys The keys of any enabled filters.
 * @param session The current session.
 * @param customTransformer The result transformer; should be null if data is not transformed before being cached.
 *
 * @return The generate query cache key.
 */
public static QueryKey generateQueryKey(
		String queryString,
		QueryParameters queryParameters,
		Set filterKeys,
		SessionImplementor session,
		CacheableResultTransformer customTransformer) {
	// disassemble positional parameters
	final int positionalParameterCount = queryParameters.getPositionalParameterTypes().length;
	final Type[] types = new Type[positionalParameterCount];
	final Object[] values = new Object[positionalParameterCount];
	for ( int i = 0; i < positionalParameterCount; i++ ) {
		types[i] = queryParameters.getPositionalParameterTypes()[i];
		values[i] = types[i].disassemble( queryParameters.getPositionalParameterValues()[i], session, null );
	}

	// disassemble named parameters
	final Map<String,TypedValue> namedParameters;
	if ( queryParameters.getNamedParameters() == null ) {
		namedParameters = null;
	}
	else {
		namedParameters = CollectionHelper.mapOfSize( queryParameters.getNamedParameters().size() );
		for ( Map.Entry<String,TypedValue> namedParameterEntry : queryParameters.getNamedParameters().entrySet() ) {
			namedParameters.put(
					namedParameterEntry.getKey(),
					new TypedValue(
							namedParameterEntry.getValue().getType(),
							namedParameterEntry.getValue().getType().disassemble(
									namedParameterEntry.getValue().getValue(),
									session,
									null
							)
					)
			);
		}
	}

	// decode row selection...
	final RowSelection selection = queryParameters.getRowSelection();
	final Integer firstRow;
	final Integer maxRows;
	if ( selection != null ) {
		firstRow = selection.getFirstRow();
		maxRows = selection.getMaxRows();
	}
	else {
		firstRow = null;
		maxRows = null;
	}

	return new QueryKey(
			queryString,
			types,
			values,
			namedParameters,
			firstRow,
			maxRows,
			filterKeys,
			session.getTenantIdentifier(),
			customTransformer
	);
}