org.hibernate.id.IdentifierGenerationException Java Examples

The following examples show how to use org.hibernate.id.IdentifierGenerationException. 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: AbstractReactiveSaveEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Serializable assignIdIfNecessary(Object generatedId, Object entity, String entityName, EventSource source) {
	EntityPersister persister = source.getEntityPersister(entityName, entity);
	if ( generatedId != null ) {
		if (generatedId instanceof Long) {
			Long longId = (Long) generatedId;
			Type identifierType = persister.getIdentifierType();
			if (identifierType == LongType.INSTANCE) {
				return longId;
			}
			else if (identifierType == IntegerType.INSTANCE) {
				return longId.intValue();
			}
			else {
				throw new HibernateException("cannot generate identifiers of type "
						+ identifierType.getReturnedClass().getSimpleName() + " for: " + entityName);
			}
		}
		else {
			return (Serializable) generatedId;
		}
	}
	else {
		Serializable assignedId = persister.getIdentifier( entity, source.getSession() );
		if (assignedId == null) {
			throw new IdentifierGenerationException("ids for this class must be manually assigned before calling save(): " + entityName);
		}
		return assignedId;
	}
}
 
Example #2
Source File: AbstractSaveEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Prepares the save call using a newly generated id.
 *
 * @param entity The entity to be saved
 * @param entityName The entity-name for the entity to be saved
 * @param anything Generally cascade-specific information.
 * @param source The session which is the source of this save event.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 *         type of id generator used and the requiresImmediateIdAccess value
 */
protected Serializable saveWithGeneratedId(
		Object entity,
		String entityName,
		Object anything,
		EventSource source,
		boolean requiresImmediateIdAccess) {
	callbackRegistry.preCreate( entity );

	if ( entity instanceof SelfDirtinessTracker ) {
		( (SelfDirtinessTracker) entity ).$$_hibernate_clearDirtyAttributes();
	}

	EntityPersister persister = source.getEntityPersister( entityName, entity );
	Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );
	if ( generatedId == null ) {
		throw new IdentifierGenerationException( "null id generated for:" + entity.getClass() );
	}
	else if ( generatedId == IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR ) {
		return source.getIdentifier( entity );
	}
	else if ( generatedId == IdentifierGeneratorHelper.POST_INSERT_INDICATOR ) {
		return performSave( entity, null, persister, true, anything, source, requiresImmediateIdAccess );
	}
	else {
		// TODO: define toString()s for generators
		if ( LOG.isDebugEnabled() ) {
			LOG.debugf(
					"Generated identifier: %s, using strategy: %s",
					persister.getIdentifierType().toLoggableString( generatedId, source.getFactory() ),
					persister.getIdentifierGenerator().getClass().getName()
			);
		}

		return performSave( entity, generatedId, persister, false, anything, source, true );
	}
}
 
Example #3
Source File: AbstractSaveEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Prepares the save call using a newly generated id.
 *
 * @param entity The entity to be saved
 * @param entityName The entity-name for the entity to be saved
 * @param anything Generally cascade-specific information.
 * @param source The session which is the source of this save event.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 *         type of id generator used and the requiresImmediateIdAccess value
 */
protected Serializable saveWithGeneratedId(
		Object entity,
		String entityName,
		Object anything,
		EventSource source,
		boolean requiresImmediateIdAccess) {
	EntityPersister persister = source.getEntityPersister( entityName, entity );
	Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );
	if ( generatedId == null ) {
		throw new IdentifierGenerationException( "null id generated for:" + entity.getClass() );
	}
	else if ( generatedId == IdentifierGeneratorFactory.SHORT_CIRCUIT_INDICATOR ) {
		return source.getIdentifier( entity );
	}
	else if ( generatedId == IdentifierGeneratorFactory.POST_INSERT_INDICATOR ) {
		return performSave( entity, null, persister, true, anything, source, requiresImmediateIdAccess );
	}
	else {

		if ( log.isDebugEnabled() ) {
			log.debug(
					"generated identifier: " +
							persister.getIdentifierType().toLoggableString( generatedId, source.getFactory() ) +
							", using strategy: " +
							persister.getIdentifierGenerator().getClass().getName()
					//TODO: define toString()s for generators
			);
		}

		return performSave( entity, generatedId, persister, false, anything, source, true );
	}
}
 
Example #4
Source File: TableStructure.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public AccessCallback buildCallback(final SharedSessionContractImplementor session) {
	final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry()
			.getService( JdbcServices.class )
			.getSqlStatementLogger();
	if ( selectQuery == null || updateQuery == null ) {
		throw new AssertionFailure( "SequenceStyleGenerator's TableStructure was not properly initialized" );
	}

	final SessionEventListenerManager statsCollector = session.getEventListenerManager();

	return new AccessCallback() {
		@Override
		public IntegralDataTypeHolder getNextValue() {
			return session.getTransactionCoordinator().createIsolationDelegate().delegateWork(
					new AbstractReturningWork<IntegralDataTypeHolder>() {
						@Override
						public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
							final IntegralDataTypeHolder value = makeValue();
							int rows;
							do {
								try (PreparedStatement selectStatement = prepareStatement(
										connection,
										selectQuery,
										statementLogger,
										statsCollector
								)) {
									final ResultSet selectRS = executeQuery( selectStatement, statsCollector );
									if ( !selectRS.next() ) {
										final String err = "could not read a hi value - you need to populate the table: " + tableNameText;
										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;
								}


								try (PreparedStatement updatePS = prepareStatement(
										connection,
										updateQuery,
										statementLogger,
										statsCollector
								)) {
									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( tableNameText, e );
									throw e;
								}
							} while ( rows == 0 );

							accessCounter++;

							return value;
						}
					},
					true
			);
		}

		@Override
		public String getTenantIdentifier() {
			return session.getTenantIdentifier();
		}
	};
}