Skip to content

Commit 7b56e18

Browse files
committed
various misc code cleanups
1 parent b515560 commit 7b56e18

File tree

5 files changed

+74
-76
lines changed

5 files changed

+74
-76
lines changed

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/AbstractMultiIdEntityLoader.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,8 @@ protected boolean loadFromEnabledCaches(
184184
EntityKey entityKey,
185185
List<Object> result,
186186
int i) {
187-
if ( loadOptions.isSessionCheckingEnabled() || loadOptions.isSecondLevelCacheCheckingEnabled() ) {
188-
return isLoadFromCaches( loadOptions, entityKey, lockOptions, result, i, session );
189-
}
190-
else {
191-
return false;
192-
}
187+
return ( loadOptions.isSessionCheckingEnabled() || loadOptions.isSecondLevelCacheCheckingEnabled() )
188+
&& isLoadFromCaches( loadOptions, entityKey, lockOptions, result, i, session );
193189
}
194190

195191
private boolean isLoadFromCaches(
@@ -321,24 +317,30 @@ private <R> List<Object> loadFromCaches(
321317
EntityKey entityKey,
322318
List<Object> unresolvedIds, int i,
323319
EventSource session) {
324-
Object cachedEntity = null;
325320

326321
// look for it in the Session first
327322
final PersistenceContextEntry persistenceContextEntry =
328323
loadFromSessionCache( entityKey, lockOptions, GET, session );
324+
final Object sessionEntity;
329325
if ( loadOptions.isSessionCheckingEnabled() ) {
330-
cachedEntity = persistenceContextEntry.entity();
331-
if ( cachedEntity != null
326+
sessionEntity = persistenceContextEntry.entity();
327+
if ( sessionEntity != null
332328
&& !loadOptions.isReturnOfDeletedEntitiesEnabled()
333329
&& !persistenceContextEntry.isManaged() ) {
334330
resolutionConsumer.consume( i, entityKey, null );
335331
return unresolvedIds;
336332
}
337333
}
334+
else {
335+
sessionEntity = null;
336+
}
338337

339-
if ( cachedEntity == null && loadOptions.isSecondLevelCacheCheckingEnabled() ) {
340-
final EntityPersister persister = getLoadable().getEntityPersister();
341-
cachedEntity = session.loadFromSecondLevelCache( persister, entityKey, null, lockOptions.getLockMode() );
338+
final Object cachedEntity;
339+
if ( sessionEntity == null && loadOptions.isSecondLevelCacheCheckingEnabled() ) {
340+
cachedEntity = session.loadFromSecondLevelCache( getLoadable().getEntityPersister(), entityKey, null, lockOptions.getLockMode() );
341+
}
342+
else {
343+
cachedEntity = sessionEntity;
342344
}
343345

344346
if ( cachedEntity != null ) {

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/MultiIdEntityLoaderArrayParam.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public LockOptions getLockOptions() {
142142
JdbcParametersList.singleton( jdbcParameter ),
143143
jdbcParameterBindings
144144
),
145-
TRUE.equals( loadOptions.getReadOnly( session ) ) ),
145+
TRUE.equals( loadOptions.getReadOnly( session ) )
146+
),
146147
RowTransformerStandardImpl.instance(),
147148
null,
148149
idsInBatch.size(),

hibernate-core/src/main/java/org/hibernate/result/internal/OutputsImpl.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ protected void executeStatement() {
6969
executeStartNanos = System.nanoTime();
7070
}
7171
final EventMonitor eventMonitor = context.getSession().getEventMonitor();
72-
final DiagnosticEvent jdbcPreparedStatementExecutionEvent = eventMonitor.beginJdbcPreparedStatementExecutionEvent();
72+
final DiagnosticEvent jdbcPreparedStatementExecutionEvent =
73+
eventMonitor.beginJdbcPreparedStatementExecutionEvent();
7374
try {
7475
final boolean isResultSet = jdbcStatement.execute();
7576
currentReturnState = buildCurrentReturnState( isResultSet );
@@ -102,19 +103,13 @@ protected CurrentReturnState buildCurrentReturnState(boolean isResultSet, int up
102103
}
103104

104105
protected JDBCException convert(SQLException e, String message) {
105-
return context.getSession().getJdbcServices().getSqlExceptionHelper().convert(
106-
e,
107-
message,
108-
jdbcStatement.toString()
109-
);
106+
return context.getSession().getJdbcServices().getSqlExceptionHelper()
107+
.convert( e, message, jdbcStatement.toString() );
110108
}
111109

112110
@Override
113111
public Output getCurrent() {
114-
if ( currentReturnState == null ) {
115-
return null;
116-
}
117-
return currentReturnState.getOutput();
112+
return currentReturnState == null ? null : currentReturnState.getOutput();
118113
}
119114

120115
@Override
@@ -154,11 +149,8 @@ private List<?> extractCurrentResults() {
154149

155150
protected List<Object> extractResults(ResultSet resultSet) {
156151

157-
final DirectResultSetAccess resultSetAccess = new DirectResultSetAccess(
158-
context.getSession(),
159-
jdbcStatement,
160-
resultSet
161-
);
152+
final DirectResultSetAccess resultSetAccess =
153+
new DirectResultSetAccess( context.getSession(), jdbcStatement, resultSet );
162154

163155
final ProcedureCallImpl<?> procedureCall = (ProcedureCallImpl<?>) context;
164156
final ResultSetMapping resultSetMapping = procedureCall.getResultSetMapping();
@@ -178,8 +170,7 @@ protected List<Object> extractResults(ResultSet resultSet) {
178170

179171
try {
180172

181-
//noinspection unchecked
182-
final RowReader<Object> rowReader = ResultsHelper.createRowReader(
173+
final RowReader<?> rowReader = ResultsHelper.createRowReader(
183174
getSessionFactory(),
184175
RowTransformerStandardImpl.instance(),
185176
null,

hibernate-core/src/main/java/org/hibernate/sql/exec/internal/StandardJdbcMutationExecutor.java

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,11 @@ public int execute(
4343
final SharedSessionContractImplementor session = executionContext.getSession();
4444
session.autoFlushIfRequired( jdbcMutation.getAffectedTableNames() );
4545

46-
final LogicalConnectionImplementor logicalConnection = session
47-
.getJdbcCoordinator()
48-
.getLogicalConnection();
46+
final LogicalConnectionImplementor logicalConnection =
47+
session.getJdbcCoordinator().getLogicalConnection();
4948

5049
final JdbcServices jdbcServices = session.getJdbcServices();
51-
final QueryOptions queryOptions = executionContext.getQueryOptions();
52-
final String finalSql;
53-
if ( queryOptions == null ) {
54-
finalSql = jdbcMutation.getSqlString();
55-
}
56-
else {
57-
finalSql = jdbcServices.getDialect().addSqlHintOrComment(
58-
jdbcMutation.getSqlString(),
59-
queryOptions,
60-
executionContext.getSession().getFactory().getSessionFactoryOptions().isCommentsEnabled()
61-
);
62-
}
50+
final String finalSql = applyOptions( jdbcMutation, executionContext, jdbcServices );
6351
try {
6452
// prepare the query
6553
final PreparedStatement preparedStatement = statementCreator.apply( finalSql );
@@ -83,9 +71,10 @@ public int execute(
8371

8472
session.getEventListenerManager().jdbcExecuteStatementStart();
8573
final EventMonitor eventMonitor = session.getEventMonitor();
86-
final DiagnosticEvent jdbcPreparedStatementExecutionEvent = eventMonitor.beginJdbcPreparedStatementExecutionEvent();
74+
final DiagnosticEvent jdbcPreparedStatementExecutionEvent =
75+
eventMonitor.beginJdbcPreparedStatementExecutionEvent();
8776
try {
88-
int rows = preparedStatement.executeUpdate();
77+
final int rows = preparedStatement.executeUpdate();
8978
expectationCheck.accept( rows, preparedStatement );
9079
return rows;
9180
}
@@ -99,33 +88,49 @@ public int execute(
9988
}
10089
}
10190
catch (SQLException e) {
102-
final JDBCException exception = jdbcServices.getSqlExceptionHelper().convert(
103-
e,
104-
"JDBC exception executing SQL [" + finalSql + "]"
105-
);
106-
if ( exception instanceof ConstraintViolationException constraintViolationException && jdbcMutation instanceof JdbcOperationQueryInsert ) {
107-
if ( constraintViolationException.getKind() == ConstraintViolationException.ConstraintKind.UNIQUE ) {
108-
final JdbcOperationQueryInsert jdbcInsert = (JdbcOperationQueryInsert) jdbcMutation;
109-
final String uniqueConstraintNameThatMayFail = jdbcInsert.getUniqueConstraintNameThatMayFail();
110-
if ( uniqueConstraintNameThatMayFail != null ) {
111-
final String violatedConstraintName = constraintViolationException.getConstraintName();
112-
if ( constraintNameMatches( uniqueConstraintNameThatMayFail, violatedConstraintName ) ) {
113-
return 0;
114-
}
115-
}
116-
}
117-
}
118-
throw exception;
91+
return handleException( jdbcMutation, e, jdbcServices, finalSql );
11992
}
12093
finally {
12194
executionContext.afterStatement( logicalConnection );
12295
}
12396
}
12497

98+
private static int handleException(
99+
JdbcOperationQueryMutation jdbcMutation, SQLException sqle, JdbcServices jdbcServices, String finalSql) {
100+
final JDBCException exception =
101+
jdbcServices.getSqlExceptionHelper()
102+
.convert( sqle, "JDBC exception executing SQL [" + finalSql + "]" );
103+
if ( exception instanceof ConstraintViolationException constraintViolationException
104+
&& jdbcMutation instanceof JdbcOperationQueryInsert jdbcInsert ) {
105+
if ( constraintViolationException.getKind() == ConstraintViolationException.ConstraintKind.UNIQUE ) {
106+
final String uniqueConstraintNameThatMayFail = jdbcInsert.getUniqueConstraintNameThatMayFail();
107+
if ( uniqueConstraintNameThatMayFail != null ) {
108+
final String violatedConstraintName = constraintViolationException.getConstraintName();
109+
if ( constraintNameMatches( uniqueConstraintNameThatMayFail, violatedConstraintName ) ) {
110+
return 0;
111+
}
112+
}
113+
}
114+
}
115+
throw exception;
116+
}
117+
118+
private static String applyOptions(
119+
JdbcOperationQueryMutation jdbcMutation, ExecutionContext executionContext, JdbcServices jdbcServices) {
120+
final QueryOptions queryOptions = executionContext.getQueryOptions();
121+
return queryOptions == null
122+
? jdbcMutation.getSqlString()
123+
: jdbcServices.getDialect().addSqlHintOrComment(
124+
jdbcMutation.getSqlString(),
125+
queryOptions,
126+
executionContext.getSession().getFactory().getSessionFactoryOptions().isCommentsEnabled()
127+
);
128+
}
129+
125130
private static boolean constraintNameMatches(String uniqueConstraintNameThatMayFail, String violatedConstraintName) {
126131
return uniqueConstraintNameThatMayFail.isEmpty()
127-
|| uniqueConstraintNameThatMayFail.equalsIgnoreCase(violatedConstraintName)
132+
|| uniqueConstraintNameThatMayFail.equalsIgnoreCase( violatedConstraintName )
128133
|| violatedConstraintName != null && violatedConstraintName.indexOf('.') > 0
129-
&& uniqueConstraintNameThatMayFail.equalsIgnoreCase(violatedConstraintName.substring(violatedConstraintName.lastIndexOf('.') + 1));
134+
&& uniqueConstraintNameThatMayFail.equalsIgnoreCase( violatedConstraintName.substring(violatedConstraintName.lastIndexOf('.') + 1) );
130135
}
131136
}

hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/DeferredResultSetAccess.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,13 @@ private static boolean useFollowOnLocking(
142142
QueryOptions queryOptions,
143143
LockOptions lockOptions,
144144
Dialect dialect) {
145-
switch ( jdbcLockStrategy ) {
146-
case FOLLOW_ON:
147-
return true;
148-
case AUTO:
149-
return lockOptions.getFollowOnLocking() == null
145+
return switch ( jdbcLockStrategy ) {
146+
case FOLLOW_ON -> true;
147+
case AUTO -> lockOptions.getFollowOnLocking() == null
150148
? dialect.useFollowOnLocking( sql, queryOptions )
151149
: lockOptions.getFollowOnLocking();
152-
default:
153-
return false;
154-
}
150+
default -> false;
151+
};
155152
}
156153

157154
public LimitHandler getLimitHandler() {
@@ -223,7 +220,8 @@ protected void bindParameters(PreparedStatement preparedStatement) throws SQLExc
223220
}
224221

225222
private void executeQuery() {
226-
final LogicalConnectionImplementor logicalConnection = getPersistenceContext().getJdbcCoordinator().getLogicalConnection();
223+
final LogicalConnectionImplementor logicalConnection =
224+
getPersistenceContext().getJdbcCoordinator().getLogicalConnection();
227225

228226
final SharedSessionContractImplementor session = executionContext.getSession();
229227
try {
@@ -241,7 +239,8 @@ private void executeQuery() {
241239
executeStartNanos = System.nanoTime();
242240
}
243241
final EventMonitor eventMonitor = session.getEventMonitor();
244-
final DiagnosticEvent jdbcPreparedStatementExecutionEvent = eventMonitor.beginJdbcPreparedStatementExecutionEvent();
242+
final DiagnosticEvent jdbcPreparedStatementExecutionEvent =
243+
eventMonitor.beginJdbcPreparedStatementExecutionEvent();
245244
try {
246245
eventListenerManager.jdbcExecuteStatementStart();
247246
resultSet = wrapResultSet( preparedStatement.executeQuery() );

0 commit comments

Comments
 (0)