类org.hibernate.id.IdentifierGenerationException源码实例Demo

下面列出了怎么用org.hibernate.id.IdentifierGenerationException的API类实例代码及写法,或者点击链接到github查看源代码。

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;
	}
}
 
源代码2 项目: lams   文件: AbstractSaveEventListener.java
/**
 * 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 );
	}
}
 
/**
 * 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 );
	}
}
 
源代码4 项目: lams   文件: TableStructure.java
@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();
		}
	};
}
 
 类所在包
 同包方法