org.hibernate.NonUniqueObjectException#org.hibernate.JDBCException源码实例Demo

下面列出了org.hibernate.NonUniqueObjectException#org.hibernate.JDBCException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: hibernate-reactive   文件: ReactiveLoader.java
default CompletionStage<List<Object>> reactiveProcessResultSet(
		ResultSet rs,
		QueryParameters queryParameters,
		SharedSessionContractImplementor session,
		boolean returnProxies,
		ResultTransformer forcedResultTransformer,
		List<AfterLoadAction> afterLoadActions) {
	try {
		return getReactiveResultSetProcessor()
				.reactiveExtractResults(
						rs,
						session,
						queryParameters,
						null,
						returnProxies,
						queryParameters.isReadOnly( session ),
						forcedResultTransformer,
						afterLoadActions
				);
	}
	catch (SQLException sqle) {
		//don't log or convert it - just pass it on to the caller
		throw new JDBCException( "could not load batch", sqle );
	}
}
 
源代码2 项目: cacheonix-core   文件: ResultCheckStyleTest.java
public void testUpdateFailureWithExceptionChecking() {
	Session s = openSession();
	s.beginTransaction();
	ExceptionCheckingEntity e = new ExceptionCheckingEntity();
	e.setId( new Long( 1 ) );
	e.setName( "dummy" );
	s.update( e );
	try {
		s.flush();
		fail( "expection flush failure!" );
	}
	catch( JDBCException ex ) {
		// these should specifically be JDBCExceptions...
	}
	s.clear();
	s.getTransaction().commit();
	s.close();
}
 
源代码3 项目: gemfirexd-oss   文件: GemFireXDDialect.java
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
  return new SQLExceptionConverter() {
    @Override
    public JDBCException convert(SQLException sqlException, String message,
        String sql) {
      final String sqlState = JDBCExceptionHelper
          .extractSqlState(sqlException);
      if (sqlState != null) {
        if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
          return new SQLGrammarException(message, sqlException, sql);
        }
        else if (DATA_CATEGORIES.contains(sqlState)) {
          return new DataException(message, sqlException, sql);
        }
        else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
          return new LockAcquisitionException(message, sqlException, sql);
        }
      }
      return null;
    }
  };
}
 
源代码4 项目: gemfirexd-oss   文件: GemFireXDDialect.java
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
        return new SQLExceptionConverter() {
                @Override
                public JDBCException convert(SQLException sqlException,
                                String message, String sql) {
                        final String sqlState = JdbcExceptionHelper
                                        .extractSqlState(sqlException);
                        if (sqlState != null) {
                                if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
                                        return new SQLGrammarException(message, sqlException,
                                                        sql);
                                } else if (DATA_CATEGORIES.contains(sqlState)) {
                                        return new DataException(message, sqlException, sql);
                                } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
                                        return new LockAcquisitionException(message,
                                                        sqlException, sql);
                                }
                        }
                        return null;
                }
        };
}
 
/**
 * Convert the given SQLException into Hibernate's JDBCException hierarchy.
 *
 * @param sqlException The SQLException to be converted.
 * @param message	  An optional error message.
 * @param sql		  Optionally, the sql being performed when the exception occurred.
 * @return The resulting JDBCException; returns null if it could not be converted.
 */
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
	String sqlStateClassCode = JdbcExceptionHelper.extractSqlStateClassCode( sqlException );
	if ( sqlStateClassCode != null ) {
		Integer errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
		if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) {
			String constraintName =
					getConversionContext()
							.getViolatedConstraintNameExtracter()
							.extractConstraintName( sqlException );
			return new ConstraintViolationException( message, sqlException, sql, constraintName );
		}
		else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
			return new DataException( message, sqlException, sql );
		}
	}
	return null; // allow other delegates the chance to look
}
 
public void testBadInsertionFails() {
	Session session = openSession();
	session.beginTransaction();
	Organization org = new Organization( "hola!" );
	try {
		session.save( org );
		session.delete( org );
		fail( "expecting bad custom insert statement to fail" );
	}
	catch( JDBCException e ) {
		// expected failure
	}

	session.getTransaction().rollback();
	session.close();
}
 
源代码7 项目: lams   文件: SybaseASE157Dialect.java
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
	return new SQLExceptionConversionDelegate() {
		@Override
		public JDBCException convert(SQLException sqlException, String message, String sql) {
			final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
			final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
			if("JZ0TO".equals( sqlState ) || "JZ006".equals( sqlState )){
				throw new LockTimeoutException( message, sqlException, sql );
			}
			if ( 515 == errorCode && "ZZZZZ".equals( sqlState ) ) {
				// Attempt to insert NULL value into column; column does not allow nulls.
				final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
				return new ConstraintViolationException( message, sqlException, sql, constraintName );
			}
			return null;
		}
	};
}
 
源代码8 项目: cacheonix-core   文件: CacheSQLStateConverter.java
/**
 * Convert the given SQLException into Hibernate's JDBCException hierarchy.
 *
 * @param sqlException The SQLException to be converted.
 * @param message	  An optional error message.
 * @param sql		  Optionally, the sql being performed when the exception occurred.
 * @return The resulting JDBCException.
 */
public JDBCException convert(SQLException sqlException, String message, String sql) {
	String sqlStateClassCode = JDBCExceptionHelper.extractSqlStateClassCode( sqlException );
	Integer errorCode = new Integer( JDBCExceptionHelper.extractErrorCode( sqlException ) );
	if ( sqlStateClassCode != null ) {
		if ( SQL_GRAMMAR_CATEGORIES.contains( sqlStateClassCode ) ) {
			return new SQLGrammarException( message, sqlException, sql );
		}
		else if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) {
			String constraintName = extracter.extractConstraintName( sqlException );
			return new ConstraintViolationException( message, sqlException, sql, constraintName );
		}
		else if ( CONNECTION_CATEGORIES.contains( sqlStateClassCode ) ) {
			return new JDBCConnectionException( message, sqlException, sql );
		}
		else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
			return new DataException( message, sqlException, sql );
		}
	}
	return handledNonSpecificException( sqlException, message, sql );
}
 
源代码9 项目: lams   文件: SQLServer2005Dialect.java
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
	return new SQLExceptionConversionDelegate() {
		@Override
		public JDBCException convert(SQLException sqlException, String message, String sql) {
			final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
			final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
			if ( "HY008".equals( sqlState ) ) {
				throw new QueryTimeoutException( message, sqlException, sql );
			}
			if (1222 == errorCode ) {
				throw new LockTimeoutException( message, sqlException, sql );
			}
			return null;
		}
	};
}
 
源代码10 项目: lams   文件: PostgreSQL81Dialect.java
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
	return new SQLExceptionConversionDelegate() {
		@Override
		public JDBCException convert(SQLException sqlException, String message, String sql) {
			final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );

			if ( "40P01".equals( sqlState ) ) {
				// DEADLOCK DETECTED
				return new LockAcquisitionException( message, sqlException, sql );
			}

			if ( "55P03".equals( sqlState ) ) {
				// LOCK NOT AVAILABLE
				return new PessimisticLockException( message, sqlException, sql );
			}

			// returning null allows other delegates to operate
			return null;
		}
	};
}
 
源代码11 项目: yeti   文件: SQLiteDialect.java
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
    return new SQLExceptionConverter() {
        @Override
        public JDBCException convert(SQLException sqlException, String message, String sql) {
            final int errorCode = sqlException.getErrorCode();
            if (errorCode == SQLITE_CONSTRAINT) {
                final String constraintName = EXTRACTER.extractConstraintName(sqlException);
                return new ConstraintViolationException(message, sqlException, sql, constraintName);
            } else if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) {
                return new DataException(message, sqlException, sql);
            } else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) {
                return new LockAcquisitionException(message, sqlException, sql);
            } else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) {
                return new JDBCConnectionException(message, sqlException, sql);
            }
            return new GenericJDBCException(message, sqlException, sql);
        }
    };
}
 
源代码12 项目: gemfirexd-oss   文件: GemFireXDDialect.java
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
  return new SQLExceptionConverter() {
    @Override
    public JDBCException convert(SQLException sqlException, String message,
        String sql) {
      final String sqlState = JDBCExceptionHelper
          .extractSqlState(sqlException);
      if (sqlState != null) {
        if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
          return new SQLGrammarException(message, sqlException, sql);
        }
        else if (DATA_CATEGORIES.contains(sqlState)) {
          return new DataException(message, sqlException, sql);
        }
        else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
          return new LockAcquisitionException(message, sqlException, sql);
        }
      }
      return null;
    }
  };
}
 
源代码13 项目: cacheonix-core   文件: SchemaExport.java
private void create(boolean script, boolean export, Writer fileOutput, Statement statement)
		throws IOException {
	for ( int j = 0; j < createSQL.length; j++ ) {
		try {
			execute( script, export, fileOutput, statement, createSQL[j] );
		}
		catch ( SQLException e ) {
			if ( haltOnError ) {
				throw new JDBCException( "Error during DDL export", e );
			}
			exceptions.add( e );
			log.error( "Unsuccessful: " + createSQL[j] );
			log.error( e.getMessage() );
		}
	}
}
 
源代码14 项目: gemfirexd-oss   文件: GemFireXDDialect.java
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
        return new SQLExceptionConversionDelegate() {
                @Override
                public JDBCException convert(SQLException sqlException,
                                String message, String sql) {
                        final String sqlState = JdbcExceptionHelper
                                        .extractSqlState(sqlException);
                        if (sqlState != null) {
                                if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
                                        return new SQLGrammarException(message, sqlException,
                                                        sql);
                                } else if (DATA_CATEGORIES.contains(sqlState)) {
                                        return new DataException(message, sqlException, sql);
                                } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
                                        return new LockAcquisitionException(message,
                                                        sqlException, sql);
                                }
                        }
                        return null;
                }
        };
}
 
/**
 * Convert the given HibernateException to an appropriate exception from the
 * {@code org.springframework.dao} hierarchy.
 * <p>Will automatically apply a specified SQLExceptionTranslator to a
 * Hibernate JDBCException, otherwise rely on Hibernate's default translation.
 * @param ex the HibernateException that occurred
 * @return a corresponding DataAccessException
 * @see SessionFactoryUtils#convertHibernateAccessException
 */
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
	if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) {
		JDBCException jdbcEx = (JDBCException) ex;
		DataAccessException dae = this.jdbcExceptionTranslator.translate(
				"Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException());
		if (dae != null) {
			throw dae;
		}
	}
	return SessionFactoryUtils.convertHibernateAccessException(ex);
}
 
default Object[] processSnapshot(SharedSessionContractImplementor session, ResultSet resultSet) {
	try {
		if ( resultSet.next() ) {
			//return the "hydrated" state (ie. associations are not resolved)
			Type[] types = getPropertyTypes();
			Object[] values = new Object[types.length];
			boolean[] includeProperty = getPropertyUpdateability();
			for ( int i = 0; i < types.length; i++ ) {
				if ( includeProperty[i] ) {
					values[i] = types[i].hydrate(
							resultSet,
							getPropertyAliases( "", i ),
							session,
							null
					); //null owner ok??
				}
			}
			return values;
		}
		else {
			//no corresponding row: transient!
			return null;
		}
	}
	catch (SQLException e) {
		//can't actually occur!
		throw new JDBCException( "error while binding parameters", e );
	}
}
 
/**
 * Convert the given HibernateException to an appropriate exception from the
 * {@code org.springframework.dao} hierarchy.
 * <p>Will automatically apply a specified SQLExceptionTranslator to a
 * Hibernate JDBCException, otherwise rely on Hibernate's default translation.
 * @param ex the HibernateException that occurred
 * @return a corresponding DataAccessException
 * @see SessionFactoryUtils#convertHibernateAccessException
 */
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
	if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) {
		JDBCException jdbcEx = (JDBCException) ex;
		DataAccessException dae = this.jdbcExceptionTranslator.translate(
				"Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException());
		if (dae != null) {
			throw dae;
		}
	}
	return SessionFactoryUtils.convertHibernateAccessException(ex);
}
 
源代码18 项目: lams   文件: BasicConnectionCreator.java
protected JDBCException convertSqlException(String message, SQLException e) {
	// if JdbcServices#getSqlExceptionHelper is available, use it...
	final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
	if ( jdbcServices != null && jdbcServices.getSqlExceptionHelper() != null ) {
		return jdbcServices.getSqlExceptionHelper().convert( e, message, null );
	}

	// likely we are still in the process of initializing the ServiceRegistry, so use the simplified
	// SQLException conversion
	return simpleConverterAccess.getValue().convert( e, message, null );
}
 
源代码19 项目: lams   文件: ContextualLobCreator.java
@Override
public Blob createBlob(byte[] bytes) {
	try {
		final Blob blob = createBlob();
		blob.setBytes( 1, bytes );
		return blob;
	}
	catch ( SQLException e ) {
		throw new JDBCException( "Unable to set BLOB bytes after creation", e );
	}
}
 
源代码20 项目: lams   文件: ContextualLobCreator.java
@Override
public NClob createNClob(String string) {
	try {
		final NClob nclob = createNClob();
		nclob.setString( 1, string );
		return nclob;
	}
	catch ( SQLException e ) {
		throw new JDBCException( "Unable to set NCLOB string after creation", e );
	}
}
 
源代码21 项目: lams   文件: SQLExceptionConverterFactory.java
/**
 * Builds a minimal converter.  The instance returned here just always converts to
 * {@link org.hibernate.exception.GenericJDBCException}.
 *
 * @return The minimal converter.
 */
public static SQLExceptionConverter buildMinimalSQLExceptionConverter() {
	return new SQLExceptionConverter() {
		public JDBCException convert(SQLException sqlException, String message, String sql) {
			return new GenericJDBCException( message, sqlException, sql );
		}
	};
}
 
源代码22 项目: lams   文件: StandardSQLExceptionConverter.java
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
	for ( SQLExceptionConversionDelegate delegate : delegates ) {
		final JDBCException jdbcException = delegate.convert( sqlException, message, sql );
		if ( jdbcException != null ) {
			return jdbcException;
		}
	}
	return new GenericJDBCException( message, sqlException, sql );
}
 
源代码23 项目: lams   文件: SQLExceptionTypeDelegate.java
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
	if ( SQLClientInfoException.class.isInstance( sqlException )
			|| SQLInvalidAuthorizationSpecException.class.isInstance( sqlException )
			|| SQLNonTransientConnectionException.class.isInstance( sqlException )
			|| SQLTransientConnectionException.class.isInstance( sqlException ) ) {
		return new JDBCConnectionException( message, sqlException, sql );
	}
	else if ( DataTruncation.class.isInstance( sqlException ) ||
			SQLDataException.class.isInstance( sqlException ) ) {
		throw new DataException( message, sqlException, sql );
	}
	else if ( SQLIntegrityConstraintViolationException.class.isInstance( sqlException ) ) {
		return new ConstraintViolationException(
				message,
				sqlException,
				sql,
				getConversionContext().getViolatedConstraintNameExtracter().extractConstraintName( sqlException )
		);
	}
	else if ( SQLSyntaxErrorException.class.isInstance( sqlException ) ) {
		return new SQLGrammarException( message, sqlException, sql );
	}
	else if ( SQLTimeoutException.class.isInstance( sqlException ) ) {
		return new QueryTimeoutException( message, sqlException, sql );
	}
	else if ( SQLTransactionRollbackException.class.isInstance( sqlException ) ) {
		// Not 100% sure this is completely accurate.  The JavaDocs for SQLTransactionRollbackException state that
		// it indicates sql states starting with '40' and that those usually indicate that:
		//		<quote>
		//			the current statement was automatically rolled back by the database because of deadlock or
		// 			other transaction serialization failures.
		//		</quote>
		return new LockAcquisitionException( message, sqlException, sql );
	}

	return null; // allow other delegates the chance to look
}
 
源代码24 项目: cacheonix-core   文件: IteratorImpl.java
public void close() throws JDBCException {
	if (ps!=null) {
		try {
			log.debug("closing iterator");
			nextResult = null;
			session.getBatcher().closeQueryStatement(ps, rs);
			ps = null;
			rs = null;
			hasNext = false;
		}
		catch (SQLException e) {
			log.info( "Unable to close iterator", e );
			throw JDBCExceptionHelper.convert(
			        session.getFactory().getSQLExceptionConverter(),
			        e,
			        "Unable to close iterator"
				);
		}
		finally {
			try {
				session.getPersistenceContext().getLoadContexts().cleanup( rs );
			}
			catch( Throwable ignore ) {
				// ignore this error for now
				log.trace( "exception trying to cleanup load context : " + ignore.getMessage() );
			}
		}
	}
}
 
源代码25 项目: lams   文件: DB2Dialect.java
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
	return new SQLExceptionConversionDelegate() {
		@Override
		public JDBCException convert(SQLException sqlException, String message, String sql) {
			final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
			final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );

			if( -952 == errorCode && "57014".equals( sqlState )){
				throw new LockTimeoutException( message, sqlException, sql );
			}
			return null;
		}
	};
}
 
源代码26 项目: cacheonix-core   文件: SQLStateConverter.java
/**
 * Convert the given SQLException into Hibernate's JDBCException hierarchy.
 *
 * @param sqlException The SQLException to be converted.
 * @param message      An optional error message.
 * @param sql          Optionally, the sql being performed when the exception occurred.
 * @return The resulting JDBCException.
 */
public JDBCException convert(SQLException sqlException, String message, String sql) {
	String sqlState = JDBCExceptionHelper.extractSqlState( sqlException );

	if ( sqlState != null ) {
		String sqlStateClassCode = JDBCExceptionHelper.determineSqlStateClassCode( sqlState );

		if ( sqlStateClassCode != null ) {
			if ( SQL_GRAMMAR_CATEGORIES.contains( sqlStateClassCode ) ) {
				return new SQLGrammarException( message, sqlException, sql );
			}
			else if ( INTEGRITY_VIOLATION_CATEGORIES.contains( sqlStateClassCode ) ) {
				String constraintName = extracter.extractConstraintName( sqlException );
				return new ConstraintViolationException( message, sqlException, sql, constraintName );
			}
			else if ( CONNECTION_CATEGORIES.contains( sqlStateClassCode ) ) {
				return new JDBCConnectionException( message, sqlException, sql );
			}
			else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
				return new DataException( message, sqlException, sql );
			}
		}

		if ( "40001".equals( sqlState ) ) {
			return new LockAcquisitionException( message, sqlException, sql );
		}

		if ( "61000".equals( sqlState ) ) {
			// oracle sql-state code for deadlock
			return new LockAcquisitionException( message, sqlException, sql );
		}
	}

	return handledNonSpecificException( sqlException, message, sql );
}
 
源代码27 项目: gemfirexd-oss   文件: GemFireXDDialect.java
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
        return new SQLExceptionConverter() {
                @Override
                public JDBCException convert(SQLException sqlException,
                                String message, String sql) {
                        final String sqlState = JdbcExceptionHelper
                                        .extractSqlState(sqlException);
                        if (sqlState != null) {
                                if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
                                        return new SQLGrammarException(message, sqlException,
                                                        sql);
                                } else if (DATA_CATEGORIES.contains(sqlState)) {
                                        return new DataException(message, sqlException, sql);
                                } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
                                        return new LockAcquisitionException(message,
                                                        sqlException, sql);
                                }
                        }
                        return null;
                }
        };
}
 
public HibernateJdbcException(JDBCException ex) {
	super("JDBC exception on Hibernate data access: SQLException for SQL [" + ex.getSQL() + "]; SQL state [" +
			ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex);
}
 
/**
 * Return the underlying SQLException.
 */
public SQLException getSQLException() {
	return ((JDBCException) getCause()).getSQLException();
}
 
/**
 * Return the SQL that led to the problem.
 */
public String getSql() {
	return ((JDBCException) getCause()).getSQL();
}