From 0878fbbddf804fd4b74afadaf788495687f32fcf Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 18 Nov 2024 16:11:40 +0100 Subject: [PATCH] fix generic typing of Consumers to make them contravariant --- .../main/java/org/hibernate/SessionFactory.java | 16 ++++++++-------- .../hibernate/internal/SessionFactoryImpl.java | 7 +++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/SessionFactory.java b/hibernate-core/src/main/java/org/hibernate/SessionFactory.java index 95f422e4e7..a0ddc6bfcc 100644 --- a/hibernate-core/src/main/java/org/hibernate/SessionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/SessionFactory.java @@ -220,7 +220,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser /** * Open a {@link Session} and use it to perform an action. */ - default void inSession(Consumer action) { + default void inSession(Consumer action) { try ( Session session = openSession() ) { action.accept( session ); } @@ -231,7 +231,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser * * @since 6.3 */ - default void inStatelessSession(Consumer action) { + default void inStatelessSession(Consumer action) { try ( StatelessSession session = openStatelessSession() ) { action.accept( session ); } @@ -244,7 +244,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser * @apiNote This method competes with the JPA-defined method * {@link #runInTransaction} */ - default void inTransaction(Consumer action) { + default void inTransaction(Consumer action) { inSession( session -> manageTransaction( session, session.beginTransaction(), action ) ); } @@ -254,14 +254,14 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser * * @since 6.3 */ - default void inStatelessTransaction(Consumer action) { + default void inStatelessTransaction(Consumer action) { inStatelessSession( session -> manageTransaction( session, session.beginTransaction(), action ) ); } /** * Open a {@link Session} and use it to obtain a value. */ - default R fromSession(Function action) { + default R fromSession(Function action) { try ( Session session = openSession() ) { return action.apply( session ); } @@ -272,7 +272,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser * * @since 6.3 */ - default R fromStatelessSession(Function action) { + default R fromStatelessSession(Function action) { try ( StatelessSession session = openStatelessSession() ) { return action.apply( session ); } @@ -285,7 +285,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser * @apiNote This method competes with the JPA-defined method * {@link #callInTransaction} */ - default R fromTransaction(Function action) { + default R fromTransaction(Function action) { return fromSession( session -> manageTransaction( session, session.beginTransaction(), action ) ); } @@ -295,7 +295,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser * * @since 6.3 */ - default R fromStatelessTransaction(Function action) { + default R fromStatelessTransaction(Function action) { return fromStatelessSession( session -> manageTransaction( session, session.beginTransaction(), action ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 04a26e20c0..d96db932bd 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -904,15 +904,14 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im return getJpaMetamodel().getNamedEntityGraphs( entityType ); } - @Override @SuppressWarnings({"unchecked", "rawtypes"}) + @Override public void runInTransaction(Consumer work) { - inTransaction( (Consumer) work ); + inTransaction( work ); } @Override public R callInTransaction(Function work) { - //noinspection unchecked,rawtypes - return (R) fromTransaction( (Function) work ); + return fromTransaction( work ); } @Override