/** * 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 == 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 ); } }
/** * 根据配置文件定义的数据类型进行类型转换 * * @param value * @param clazz * @return * @throws IdentifierGenerationException */ private Serializable createId(long value, Class<?> clazz) throws IdentifierGenerationException { if (clazz == Long.class) { return new Long(value); } else if (clazz == Integer.class) { return new Integer((int) value); } else if (clazz == Short.class) { return new Short((short) value); } else { throw new IdentifierGenerationException("this id generator generates long, integer, short"); } }
@Transactional public void saveOrUpdate(T object) { try { getSession().saveOrUpdate(object); } catch (ConstraintViolationException | PropertyValueException | IdentifierGenerationException exception) { throw new IllegalArgumentException(exception); } }
@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(); } }; }
public static void rethrowJdbcAsDatastoreAndConstraintAsItself( final HibernateException he, final Log log, final String message, final int logErrorMask ) throws DatastoreException, MAConstraintViolationException { final String internalMess = concMess( message, he ); if (he instanceof JDBCConnectionException || he instanceof GenericJDBCException || he instanceof SQLGrammarException) { if ((logErrorMask & JDBC_IS_ERROR) != 0) { log.error( internalMess, he ); } throw new DatastoreException( internalMess, he ); } else if (he instanceof ConstraintViolationException) { if ((logErrorMask & CONSTRAINT_IS_ERROR) != 0) { log.error( internalMess, he ); } throw new MAConstraintViolationException( internalMess, he ); } else if (he instanceof LockAcquisitionException) { log.error( internalMess, he ); throw new DatastoreException( internalMess, he ); } else if (he instanceof QuerySyntaxException) { log.error( internalMess, he ); throw new DatastoreException( internalMess, he ); } else if (he instanceof IdentifierGenerationException) { log.error( internalMess, he ); throw new DatastoreException( internalMess, he ); } else if (he instanceof PropertyValueException) { log.error( internalMess, he ); throw new DatastoreException( internalMess, he ); } else { log.error( internalMess, he ); throw new DatastoreException( internalMess, he ); } }