-
-
Couldn't load subscription status.
- Fork 3.7k
HHH-19849 Add an SPI that allows attaching session-scoped "extensions" to the session/statelesssession implementors #11093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marko-bekhta
wants to merge
1
commit into
hibernate:main
Choose a base branch
from
marko-bekhta:feat/HHH-19849-Add-an-SPI-that-allows-attaching-session-scoped-extensions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
hibernate-core/src/main/java/org/hibernate/engine/spi/ExtensionStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.engine.spi; | ||
|
|
||
| import org.hibernate.Incubating; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Marker interface for extensions to register themselves within a session instance. | ||
| * | ||
| * @see SharedSessionContractImplementor#getExtensionStorage(Class, Supplier) | ||
| */ | ||
| @Incubating | ||
| public interface ExtensionStorage { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
hibernate-core/src/test/java/org/hibernate/orm/test/engine/spi/SessionExtensionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.engine.spi; | ||
|
|
||
| import jakarta.persistence.Id; | ||
| import org.hibernate.engine.spi.ExtensionStorage; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| @DomainModel(annotatedClasses = { | ||
| SessionExtensionTest.UselessEntity.class, | ||
| }) | ||
| @SessionFactory | ||
| public class SessionExtensionTest { | ||
|
|
||
| @Test | ||
| public void failing(SessionFactoryScope scope) { | ||
| scope.inSession( sessionImplementor -> { | ||
| assertThatThrownBy( | ||
| () -> sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, MySometimesFailingExtensionStorage::new ) ) | ||
| .isInstanceOf( UnsupportedOperationException.class ); | ||
| } ); | ||
|
|
||
| scope.inStatelessSession( sessionImplementor -> { | ||
| assertThatThrownBy( | ||
| () -> sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, MySometimesFailingExtensionStorage::new ) ) | ||
| .isInstanceOf( UnsupportedOperationException.class ); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| public void supplier(SessionFactoryScope scope) { | ||
| scope.inSession( sessionImplementor -> { | ||
| sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, | ||
| () -> new MySometimesFailingExtensionStorage( new HashMap<>() ) ) | ||
| .add( new Extension( 1 ) ); | ||
|
|
||
| assertThat( sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, MySometimesFailingExtensionStorage::new ).get( 1 ) ) | ||
| .isNotNull() | ||
| .isEqualTo( new Extension( 1 ) ); | ||
|
|
||
| assertThat( sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, | ||
| () -> new MySometimesFailingExtensionStorage( new HashMap<>() ) ).get( 1 ) ) | ||
| .isNotNull() | ||
| .isEqualTo( new Extension( 1 ) ); | ||
| } ); | ||
|
|
||
| scope.inStatelessSession( sessionImplementor -> { | ||
| sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, | ||
| () -> new MySometimesFailingExtensionStorage( new HashMap<>() ) ) | ||
| .add( new Extension( 1 ) ); | ||
|
|
||
| assertThat( sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, MySometimesFailingExtensionStorage::new ).get( 1 ) ) | ||
| .isNotNull() | ||
| .isEqualTo( new Extension( 1 ) ); | ||
|
|
||
| assertThat( sessionImplementor.getExtensionStorage( MySometimesFailingExtensionStorage.class, | ||
| () -> new MySometimesFailingExtensionStorage( new HashMap<>() ) ).get( 1 ) ) | ||
| .isNotNull() | ||
| .isEqualTo( new Extension( 1 ) ); | ||
| } ); | ||
| } | ||
|
|
||
| public static class MySometimesFailingExtensionStorage implements ExtensionStorage { | ||
| Map<Integer, Extension> extensions = new HashMap<>(); | ||
|
|
||
| public MySometimesFailingExtensionStorage() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| MySometimesFailingExtensionStorage(Map<Integer, Extension> extensions) { | ||
| this.extensions = extensions; | ||
| } | ||
|
|
||
| public void add(Extension extension) { | ||
| extensions.put( extension.number, extension ); | ||
| } | ||
|
|
||
| public Extension get(int number) { | ||
| return extensions.get( number ); | ||
| } | ||
| } | ||
|
|
||
| public record Extension(int number) { | ||
| } | ||
|
|
||
| static class UselessEntity { | ||
| @Id | ||
| Long id; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perf tip:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really, this performs better?
That's the code I would use by default (because it's obviously cleaner) but I've had people complain that it doesn't perform well due to the extra lambda.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah... I didn't want to create unnecessary lambdas, but if you are saying this should perform better 👍🏻 🙂 thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha 🙂, now @mbellade we need to benchmark it 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so I got curious how much that lambda would hurt ...
I'll keep the get for now 🫣 🙂
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this was run with the same key every time, such that
putis only invoked once. The instantiation cost of the lambda of course will have an impact, but accessing an hash-map twice (onegetand oneput) should be much worse if the entry doesn't exist yet.So it really depends on the use-case :)
Edit: if there wasn't a
Supplierbut anObjectinstead,putIfAbsentwould be the winner regardless of the use case.