-
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
Open
NickBeginner
wants to merge
9
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.
+301
−4
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b97e0f0
feat: issue-32936 add support for TypeContributor and FunctionContrib…
NickBeginner fe2099a
feat: wip ISSUE-32936 adding support for AdditionalMappingContributions
NickBeginner ccfa85c
Revert "feat: wip ISSUE-32936 adding support for AdditionalMappingCon…
NickBeginner b48f7a1
wip tests
NickBeginner 69d64cf
add type contributor test
NickBeginner 53fc714
deprecate metadatbuildercontributor
NickBeginner 84064e0
add documentation
NickBeginner aeefabf
apply formatting
NickBeginner 11f1506
fix formatting
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
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
60 changes: 60 additions & 0 deletions
60
...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,60 @@ | ||
| package io.quarkus.hibernate.orm.functioncontributors; | ||
|
|
||
| import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| 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 io.quarkus.hibernate.orm.PersistenceUnitExtension; | ||
|
|
||
| @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 + "')"); | ||
| } | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
.../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,35 @@ | ||
| package io.quarkus.hibernate.orm.functioncontributors; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| 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 io.quarkus.test.QuarkusUnitTest; | ||
|
|
||
| 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"); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...-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,31 @@ | ||
| 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; | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
.../deployment/src/test/java/io/quarkus/hibernate/orm/typecontributors/BooleanYesNoType.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,52 @@ | ||
| package io.quarkus.hibernate.orm.typecontributors; | ||
|
|
||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|
|
||
| import org.hibernate.type.SqlTypes; | ||
| import org.hibernate.type.descriptor.WrapperOptions; | ||
| import org.hibernate.usertype.UserType; | ||
|
|
||
| final class BooleanYesNoType implements UserType<Boolean> { | ||
|
|
||
| @Override | ||
| public int getSqlType() { | ||
| return SqlTypes.VARCHAR; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<Boolean> returnedClass() { | ||
| return Boolean.class; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean nullSafeGet(ResultSet rs, int position, WrapperOptions options) throws SQLException { | ||
| String value = rs.getString(position); | ||
|
|
||
| if (value == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return "Y".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value); | ||
| } | ||
|
|
||
| @Override | ||
| public void nullSafeSet(PreparedStatement st, Boolean value, int position, WrapperOptions options) throws SQLException { | ||
| if (value == null) { | ||
| st.setNull(position, SqlTypes.VARCHAR); | ||
| } else { | ||
| st.setString(position, value ? "Y" : "N"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean deepCopy(Boolean value) { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isMutable() { | ||
| return false; | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
...oyment/src/test/java/io/quarkus/hibernate/orm/typecontributors/CustomTypeContributor.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,20 @@ | ||
| package io.quarkus.hibernate.orm.typecontributors; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
|
|
||
| import org.hibernate.boot.model.TypeContributions; | ||
| import org.hibernate.boot.model.TypeContributor; | ||
| import org.hibernate.service.ServiceRegistry; | ||
|
|
||
| import io.quarkus.hibernate.orm.PersistenceUnitExtension; | ||
|
|
||
| @ApplicationScoped | ||
| @PersistenceUnitExtension | ||
| public class CustomTypeContributor implements TypeContributor { | ||
| @Override | ||
| public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { | ||
| typeContributions.getTypeConfiguration() | ||
| .getBasicTypeRegistry() | ||
| .register(new BooleanYesNoType(), "boolean_yes_no"); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...nate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/typecontributors/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,27 @@ | ||
| package io.quarkus.hibernate.orm.typecontributors; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
|
|
||
| import org.hibernate.annotations.Type; | ||
|
|
||
| @Entity | ||
| public class MyEntity { | ||
| @Id | ||
| private long id; | ||
|
|
||
| @Type(value = BooleanYesNoType.class) | ||
| public Boolean active; | ||
|
|
||
| public MyEntity() { | ||
| } | ||
|
|
||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
...ployment/src/test/java/io/quarkus/hibernate/orm/typecontributors/TypeContributorTest.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,43 @@ | ||
| package io.quarkus.hibernate.orm.typecontributors; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| 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 io.quarkus.test.QuarkusUnitTest; | ||
|
|
||
| public class TypeContributorTest { | ||
| @RegisterExtension | ||
| static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
| .withApplicationRoot(jar -> jar | ||
| .addClass(MyEntity.class) | ||
| .addClass(BooleanYesNoType.class) | ||
| .addClass(CustomTypeContributor.class)); | ||
|
|
||
| @Inject | ||
| EntityManager entityManager; | ||
|
|
||
| @Test | ||
| @Transactional | ||
| public void testCustomType() { | ||
| MyEntity entity = new MyEntity(); | ||
| entity.active = true; | ||
|
|
||
| entityManager.persist(entity); | ||
| entityManager.flush(); | ||
| entityManager.clear(); | ||
|
|
||
| MyEntity savedEntity = entityManager.find(MyEntity.class, entity.getId()); | ||
|
|
||
| assertThat(savedEntity.active).isTrue(); | ||
|
|
||
| String savedValue = (String) entityManager.createNativeQuery("SELECT active FROM MyEntity WHERE id = " + entity.getId()) | ||
| .getSingleResult(); | ||
| assertThat(savedValue).isEqualTo("Y"); | ||
| } | ||
| } |
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
Oops, something went wrong.
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
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.
@yrodiere i added the new components to the list, but i did not create the pages dedicate to them, is it okay or do you want me also to add the pages?
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 think a section dedicated to "Custom functions and types" would make sense, so that it appears in the table of contents and people can easily find how to register those. But you probably don't need detailed explanation beyond one sentence and an example. Does that make sense?
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.
make perfect sense, gonna work on it this weekend. Thanks :)