-
Notifications
You must be signed in to change notification settings - Fork 3k
Introduce support for hibernate.type.preferred_*_jdbc_type properties
#50454
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
Merged
+297
−5
Merged
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
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
24 changes: 24 additions & 0 deletions
24
...o/quarkus/it/jpa/preferredhibernatetypesoverride/EntityWithOverridablePreferredTypes.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,24 @@ | ||
| package io.quarkus.it.jpa.preferredhibernatetypesoverride; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.UUID; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.Id; | ||
|
|
||
| @Entity(name = EntityWithOverridablePreferredTypes.NAME) | ||
| public class EntityWithOverridablePreferredTypes { | ||
| public static final String NAME = "ent_with_preferred_types"; | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| public UUID id; | ||
|
|
||
| public Instant createdAt; | ||
|
|
||
| public boolean isPersisted; | ||
|
|
||
| public Duration overridenDuration; | ||
| } |
100 changes: 100 additions & 0 deletions
100
...a/io/quarkus/it/jpa/preferredhibernatetypesoverride/OverriddenPreferredTypesResource.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,100 @@ | ||
| package io.quarkus.it.jpa.preferredhibernatetypesoverride; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.transaction.Transactional; | ||
| import jakarta.ws.rs.GET; | ||
| import jakarta.ws.rs.Path; | ||
| import jakarta.ws.rs.Produces; | ||
| import jakarta.ws.rs.core.MediaType; | ||
|
|
||
| import org.hibernate.Session; | ||
| import org.hibernate.engine.spi.SessionFactoryImplementor; | ||
| import org.hibernate.type.SqlTypes; | ||
| import org.hibernate.type.StandardBasicTypes; | ||
|
|
||
| import io.quarkus.hibernate.orm.PersistenceUnit; | ||
|
|
||
| @Path("/overridden-preferred-types") | ||
| @ApplicationScoped | ||
| public class OverriddenPreferredTypesResource { | ||
|
|
||
| @Inject | ||
| @PersistenceUnit("overridden-types") | ||
| Session session; | ||
|
|
||
| @GET | ||
| @Path("/test-successful-persistence") | ||
| @Produces(MediaType.TEXT_PLAIN) | ||
| @Transactional | ||
| public String testSuccessfulPersistence() { | ||
| var entity = persistEntity(); | ||
|
|
||
| assertThat(findUsingNativeQuery()).contains(entity.id); | ||
|
|
||
| return "OK"; | ||
| } | ||
|
|
||
| @GET | ||
| @Path("/test-successful-override") | ||
| @Produces(MediaType.TEXT_PLAIN) | ||
| @Transactional | ||
| public String testSuccessfulPreferredTypesOverride() { | ||
| persistEntity(); | ||
|
|
||
| var metamodel = session.getFactory() | ||
| .unwrap(SessionFactoryImplementor.class) | ||
| .getMappingMetamodel() | ||
| .findEntityDescriptor(EntityWithOverridablePreferredTypes.class); | ||
|
|
||
| assertThat(metamodel.getIdentifierMapping().getSingleJdbcMapping().getJdbcType() | ||
| .getDefaultSqlTypeCode()) | ||
| .isEqualTo(SqlTypes.CHAR); | ||
| assertThat(metamodel.getAttributeMapping(metamodel.getPropertyIndex("createdAt")).getSingleJdbcMapping().getJdbcType() | ||
| .getDefaultSqlTypeCode()) | ||
| .isEqualTo(SqlTypes.INSTANT); | ||
|
|
||
| assertThat(metamodel.getAttributeMapping(metamodel.getPropertyIndex("overridenDuration")).getSingleJdbcMapping() | ||
| .getJdbcType() | ||
| .getDefaultSqlTypeCode()) | ||
| .isEqualTo(SqlTypes.INTERVAL_SECOND); | ||
|
|
||
| // Cannot detect BIT from the metamodel because it's handled as Boolean at runtime | ||
| // See https://github.com/hibernate/hibernate-orm/blob/018b8eeda3627e114ec25bd48407ccb9c47564ce/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/BooleanJdbcType.java#L61-L66 | ||
| assertThat(metamodel.getAttributeMapping(metamodel.getPropertyIndex("isPersisted")).getSingleJdbcMapping().getJdbcType() | ||
| .getDefaultSqlTypeCode()) | ||
| .isEqualTo(SqlTypes.BOOLEAN); | ||
|
|
||
| return "OK"; | ||
| } | ||
|
|
||
| private EntityWithOverridablePreferredTypes persistEntity() { | ||
| var entity = new EntityWithOverridablePreferredTypes(); | ||
| entity.isPersisted = true; | ||
| entity.createdAt = Instant.now(); | ||
| entity.overridenDuration = Duration.ofDays(1); | ||
|
|
||
| session.persist(entity); | ||
| session.flush(); | ||
| session.clear(); | ||
|
|
||
| return entity; | ||
| } | ||
|
|
||
| private List<UUID> findUsingNativeQuery() { | ||
| return session.createNativeQuery( | ||
| """ | ||
| SELECT id FROM %s WHERE isPersisted = :isPersisted | ||
| """.formatted(EntityWithOverridablePreferredTypes.NAME)) | ||
| .addScalar("id", StandardBasicTypes.UUID_CHAR) | ||
| .setParameter("isPersisted", true) | ||
| .getResultList(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
integration-tests/jpa/src/main/resources/application.properties
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 |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| quarkus.datasource.db-kind=h2 | ||
| quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test | ||
| quarkus.hibernate-orm.packages=io.quarkus.it.jpa | ||
|
|
||
| quarkus.hibernate-orm.schema-management.strategy=drop-and-create | ||
|
|
||
| quarkus.hibernate-orm.metadata-builder-contributor=io.quarkus.it.jpa.defaultcatalogandschema.Schema1MetadataBuilderContributor | ||
|
|
||
| quarkus.hibernate-orm.multitenant=DISCRIMINATOR | ||
|
|
||
| quarkus.datasource."overridden-types".db-kind=h2 | ||
| quarkus.hibernate-orm."overridden-types".datasource=overridden-types | ||
| quarkus.hibernate-orm."overridden-types".packages=io.quarkus.it.jpa.preferredhibernatetypesoverride | ||
| quarkus.hibernate-orm."overridden-types".mapping.duration.preferred-jdbc-type=INTERVAL_SECOND | ||
| quarkus.hibernate-orm."overridden-types".mapping.instant.preferred-jdbc-type=INSTANT | ||
| quarkus.hibernate-orm."overridden-types".mapping.boolean.preferred-jdbc-type=BIT | ||
| quarkus.hibernate-orm."overridden-types".mapping.uuid.preferred-jdbc-type=CHAR | ||
|
|
||
| dummy.transaction.timeout=30 |
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
7 changes: 7 additions & 0 deletions
7
...quarkus/it/jpa/preferredhibernatetypesoverride/OverriddenPreferredTypesInGraalITCase.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,7 @@ | ||
| package io.quarkus.it.jpa.preferredhibernatetypesoverride; | ||
|
|
||
| import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
|
||
| @QuarkusIntegrationTest | ||
| public class OverriddenPreferredTypesInGraalITCase extends OverriddenPreferredTypesTest { | ||
| } |
26 changes: 26 additions & 0 deletions
26
.../java/io/quarkus/it/jpa/preferredhibernatetypesoverride/OverriddenPreferredTypesTest.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,26 @@ | ||
| package io.quarkus.it.jpa.preferredhibernatetypesoverride; | ||
|
|
||
| import static io.restassured.RestAssured.given; | ||
| import static org.hamcrest.core.Is.is; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.quarkus.test.junit.QuarkusTest; | ||
|
|
||
| @QuarkusTest | ||
| class OverriddenPreferredTypesTest { | ||
|
|
||
| @Test | ||
| void shouldSaveEntityWithOverriddenTypes() { | ||
| given().when().get("/jpa-test/overridden-preferred-types/test-successful-persistence").then() | ||
| .body(is("OK")) | ||
| .statusCode(200); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldOverrideTypes() { | ||
| given().when().get("/jpa-test/overridden-preferred-types/test-successful-override").then() | ||
| .body(is("OK")) | ||
| .statusCode(200); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.