-
Notifications
You must be signed in to change notification settings - Fork 3k
ISSUE-32936 | Allow plugging in Hibernate ORM's TypeContributor and FunctionContributor #51323
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
Draft
NickBeginner
wants to merge
4
commits into
quarkusio:main
Choose a base branch
from
NickBeginner:nbelcastro/issue-32936
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.
+156
−6
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3fde94a
feat: issue-32936 add support for TypeContributor and FunctionContrib…
NickBeginner 2bc8557
feat: wip ISSUE-32936 adding support for AdditionalMappingContributions
NickBeginner 42ed098
Revert "feat: wip ISSUE-32936 adding support for AdditionalMappingCon…
NickBeginner 69d402c
wip tests
NickBeginner 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
...rc/test/java/io/quarkus/hibernate/orm/functioncontributors/CustomFunctionContributor.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,63 @@ | ||
| package io.quarkus.hibernate.orm.functioncontributors; | ||
|
|
||
| import io.quarkus.hibernate.orm.PersistenceUnitExtension; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import org.hibernate.boot.model.FunctionContributions; | ||
| import org.hibernate.boot.model.FunctionContributor; | ||
| import org.hibernate.metamodel.model.domain.ReturnableType; | ||
| import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor; | ||
| import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; | ||
| import org.hibernate.query.sqm.produce.function.StandardFunctionArgumentTypeResolvers; | ||
| import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; | ||
| import org.hibernate.sql.ast.SqlAstNodeRenderingMode; | ||
| import org.hibernate.sql.ast.SqlAstTranslator; | ||
| import org.hibernate.sql.ast.spi.SqlAppender; | ||
| import org.hibernate.sql.ast.tree.SqlAstNode; | ||
| import org.hibernate.type.StandardBasicTypes; | ||
| import org.hibernate.type.spi.TypeConfiguration; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING; | ||
|
|
||
| @ApplicationScoped | ||
| @PersistenceUnitExtension | ||
| public class CustomFunctionContributor implements FunctionContributor { | ||
| @Override | ||
| public void contributeFunctions(FunctionContributions functionContributions) { | ||
| functionContributions.getFunctionRegistry().register( | ||
| "addHardcodedSuffix", | ||
| new HardcodedSuffixFunction( | ||
| functionContributions.getTypeConfiguration(), "_some_suffix" | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private static final class HardcodedSuffixFunction extends AbstractSqmSelfRenderingFunctionDescriptor { | ||
| private final String suffix; | ||
|
|
||
| private HardcodedSuffixFunction(TypeConfiguration typeConfiguration, String suffix) { | ||
| super("addHardcodedSuffix", | ||
| StandardArgumentsValidators.exactly(1), | ||
| StandardFunctionReturnTypeResolvers.invariant( | ||
| typeConfiguration.getBasicTypeRegistry().resolve(StandardBasicTypes.STRING) | ||
| ), | ||
| StandardFunctionArgumentTypeResolvers.impliedOrInvariant(typeConfiguration, STRING) | ||
| ); | ||
|
|
||
| this.suffix = suffix; | ||
| } | ||
|
|
||
| @Override | ||
| public void render( | ||
| SqlAppender sqlAppender, | ||
| List<? extends SqlAstNode> sqlAstArguments, | ||
| ReturnableType<?> returnType, | ||
| SqlAstTranslator<?> walker | ||
| ) { | ||
| sqlAppender.appendSql('('); | ||
| walker.render(sqlAstArguments.get(0), SqlAstNodeRenderingMode.DEFAULT); | ||
| sqlAppender.appendSql(" || '" + suffix + "')"); | ||
| } | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
.../src/test/java/io/quarkus/hibernate/orm/functioncontributors/FunctionContributorTest.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,33 @@ | ||
| package io.quarkus.hibernate.orm.functioncontributors; | ||
|
|
||
| import io.quarkus.test.QuarkusUnitTest; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.persistence.EntityManager; | ||
| import jakarta.transaction.Transactional; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class FunctionContributorTest { | ||
| @RegisterExtension | ||
| static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
| .withApplicationRoot((jar) -> jar | ||
| .addClass(MyEntity.class) | ||
| .addClass(CustomFunctionContributor.class)); | ||
|
|
||
| @Inject | ||
| EntityManager entityManager; | ||
|
|
||
| @Test | ||
| @Transactional | ||
| public void test() { | ||
| MyEntity entity = new MyEntity(); | ||
| entity.setName("some_name"); | ||
| entityManager.persist(entity); | ||
|
|
||
| assertThat(entityManager.createQuery("select addHardcodedSuffix(e.name) from MyEntity e", String.class) | ||
| .getSingleResult()) | ||
| .isEqualTo("some_name_some_suffix"); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
...-orm/deployment/src/test/java/io/quarkus/hibernate/orm/functioncontributors/MyEntity.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,30 @@ | ||
| package io.quarkus.hibernate.orm.functioncontributors; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
|
|
||
| @Entity | ||
| public class MyEntity { | ||
| @Id | ||
| private long id; | ||
|
|
||
| private String name; | ||
|
|
||
| public MyEntity() {} | ||
|
|
||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
| } |
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
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.
Note you'll need to update docs as well, to mention the new component types in that list:
quarkus/docs/src/main/asciidoc/hibernate-orm.adoc
Lines 543 to 561 in 206858a