From e48a8120a93e1f42c3e7bb7b4e1b20dcd664e471 Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 1 Jan 2023 16:11:08 +0100 Subject: [PATCH] improve some error messages --- .../internal/DefaultLoadEventListener.java | 7 +++--- .../jdbc/internal/JdbcValuesCacheHit.java | 23 +++++++++++++++---- .../internal/JdbcValuesResultSetImpl.java | 18 +++++++-------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java index 4b97771d38..f9daebfa55 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java @@ -18,7 +18,6 @@ import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.engine.spi.Status; import org.hibernate.event.spi.EventSource; import org.hibernate.event.spi.LoadEvent; import org.hibernate.event.spi.LoadEventListener; @@ -65,9 +64,9 @@ public class DefaultLoadEventListener implements LoadEventListener { final Class idClass = persister.getIdentifierType().getReturnedClass(); if ( handleIdType( persister, event, loadType, idClass) ) { throw new TypeMismatchException( - "Provided id of the wrong type for class " + persister.getEntityName() - + ". Expected: " + idClass - + ", got " + event.getEntityId().getClass() + "Supplied id had wrong type: entity '" + persister.getEntityName() + + "' has id type '" + idClass + + "' but supplied id was of type '" + event.getEntityId().getClass() + "'" ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java index 65325455e8..ef04cd1487 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java @@ -16,7 +16,7 @@ import org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata; import org.hibernate.sql.results.jdbc.spi.RowProcessingState; /** - * A JdbcValuesSource implementation for cases where we had a cache hit. + * An {@link AbstractJdbcValues} implementation for cases where we had a cache hit. * * @author Steve Ebersole */ @@ -71,7 +71,11 @@ public class JdbcValuesCacheHit extends AbstractJdbcValues { @Override protected boolean processNext(RowProcessingState rowProcessingState) { - ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( "JdbcValuesCacheHit#processNext : position = %i; numberOfRows = %i", position, numberOfRows ); + ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( + "JdbcValuesCacheHit#processNext : position = %i; numberOfRows = %i", + position, + numberOfRows + ); // NOTE : explicitly skipping limit handling because the cached state ought // already be the limited size since the cache key includes limits @@ -88,7 +92,10 @@ public class JdbcValuesCacheHit extends AbstractJdbcValues { @Override protected boolean processPrevious(RowProcessingState rowProcessingState) { - ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( "JdbcValuesCacheHit#processPrevious : position = %i; numberOfRows = %i", position, numberOfRows ); + ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( + "JdbcValuesCacheHit#processPrevious : position = %i; numberOfRows = %i", + position, numberOfRows + ); // NOTE : explicitly skipping limit handling because the cached state ought // already be the limited size since the cache key includes limits @@ -105,7 +112,10 @@ public class JdbcValuesCacheHit extends AbstractJdbcValues { @Override protected boolean processScroll(int numberOfRows, RowProcessingState rowProcessingState) { - ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( "JdbcValuesCacheHit#processScroll(%i) : position = %i; numberOfRows = %i", numberOfRows, position, this.numberOfRows ); + ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( + "JdbcValuesCacheHit#processScroll(%i) : position = %i; numberOfRows = %i", + numberOfRows, position, this.numberOfRows + ); // NOTE : explicitly skipping limit handling because the cached state should // already be the limited size since the cache key includes limits @@ -127,7 +137,10 @@ public class JdbcValuesCacheHit extends AbstractJdbcValues { @Override protected boolean processPosition(int position, RowProcessingState rowProcessingState) { - ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( "JdbcValuesCacheHit#processPosition(%i) : position = %i; numberOfRows = %i", position, this.position, this.numberOfRows ); + ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef( + "JdbcValuesCacheHit#processPosition(%i) : position = %i; numberOfRows = %i", + position, this.position, this.numberOfRows + ); // NOTE : explicitly skipping limit handling because the cached state should // already be the limited size since the cache key includes limits diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java index 75559339a3..25aa666fd0 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java @@ -10,7 +10,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; -import org.hibernate.HibernateException; import org.hibernate.cache.spi.QueryKey; import org.hibernate.cache.spi.QueryResultsCache; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -27,7 +26,7 @@ import org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata; import org.hibernate.sql.results.jdbc.spi.RowProcessingState; /** - * JdbcValuesSource implementation for a JDBC ResultSet as the source + * {@link AbstractJdbcValues} implementation for a JDBC {@link ResultSet} as the source * * @author Steve Ebersole */ @@ -247,7 +246,7 @@ public class JdbcValuesResultSetImpl extends AbstractJdbcValues { private ExecutionException makeExecutionException(String message, SQLException cause) { return new ExecutionException( - message, + message + " [" + cause.getMessage() + "]", executionContext.getSession().getJdbcServices().getSqlExceptionHelper().convert( cause, message @@ -266,10 +265,11 @@ public class JdbcValuesResultSetImpl extends AbstractJdbcValues { session ); } - catch (Exception e) { - throw new HibernateException( - "Unable to extract JDBC value for position `" + sqlSelection.getJdbcResultSetIndex() + "`", - e + catch ( SQLException e ) { + // do not want to wrap in ExecutionException here + throw executionContext.getSession().getJdbcServices().getSqlExceptionHelper().convert( + e, + "Could not extract column [" + sqlSelection.getJdbcResultSetIndex() + "] from JDBC ResultSet" ); } } @@ -293,9 +293,9 @@ public class JdbcValuesResultSetImpl extends AbstractJdbcValues { @Override public void setFetchSize(int fetchSize) { try { - resultSetAccess.getResultSet().setFetchSize(fetchSize); + resultSetAccess.getResultSet().setFetchSize( fetchSize ); } - catch (SQLException e) { + catch ( SQLException e ) { throw makeExecutionException( "Error calling ResultSet.setFetchSize()", e ); } }