From 1de4a760080c08a6ba7da7d2fa423d0f906458d3 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 6 Apr 2023 11:19:56 +0100 Subject: [PATCH] HHH-16444 When logging the selected dialect, log the db version too The db version used by the dialect makes a huge different on the sql queries used. The log before this commit: ``` INFO SQL dialect [vert.x-worker-thread-0] HHH000400: Using dialect: org.hibernate.dialect.MariaDBDialect ``` The log after this commit: ``` INFO SQL dialect [vert.x-worker-thread-0] HHH000400: Using dialect: org.hibernate.dialect.MariaDBDialect, version: 10.11 ``` I've also updated the HANADialectTestCase because now the error message contains the db version --- .../java/org/hibernate/dialect/Dialect.java | 2 +- .../dialect/SimpleDatabaseVersion.java | 17 ++++++++++++++ .../orm/test/dialect/HANADialectTestCase.java | 23 ++++++++----------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java index 8e10a7551b..9ea9e32a9b 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -1528,7 +1528,7 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun @Override public String toString() { - return getClass().getName(); + return getClass().getName() + ", version: " + getVersion(); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SimpleDatabaseVersion.java b/hibernate-core/src/main/java/org/hibernate/dialect/SimpleDatabaseVersion.java index 1d7fab37ca..7702037b02 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SimpleDatabaseVersion.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SimpleDatabaseVersion.java @@ -84,4 +84,21 @@ public class SimpleDatabaseVersion implements DatabaseVersion { public int getMicro() { return micro; } + + @Override + public String toString() { + StringBuilder version = new StringBuilder(); + if ( major != NO_VERSION ) { + version.append( major ); + } + if ( minor != NO_VERSION ) { + version.append( "." ); + version.append( minor ); + if ( micro > 0 ) { + version.append( "." ); + version.append( micro ); + } + } + return version.toString(); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/HANADialectTestCase.java b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/HANADialectTestCase.java index fe255390ac..24deb7b6ed 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/HANADialectTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/HANADialectTestCase.java @@ -29,11 +29,10 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import org.hamcrest.MatcherAssert; -import static org.hamcrest.core.Is.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HANADialectTestCase extends BaseUnitTestCase { @Test @@ -42,20 +41,18 @@ public class HANADialectTestCase extends BaseUnitTestCase { () -> new StandardServiceRegistryBuilder() .applySetting( AvailableSettings.DIALECT, HANAColumnStoreDialect.class ) .build(), - (registryScope) -> { + registryScope -> { final StandardServiceRegistry registry = registryScope.getRegistry(); final MetadataSources metadataSources = new MetadataSources( registry ); metadataSources.addAnnotatedClass( EntityWithIdentity.class ); - try ( SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) metadataSources.buildMetadata().buildSessionFactory() ) { - fail( "Should have thrown MappingException!" ); - } - catch (MappingException e) { - MatcherAssert.assertThat( - e.getMessage(), - is( "The INSERT statement for table [EntityWithIdentity] contains no column, and this is not supported by [" + HANAColumnStoreDialect.class.getName() + "]" ) - ); - } + String errorMessage = assertThrows( MappingException.class, () -> { + try ( SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) metadataSources.buildMetadata().buildSessionFactory() ) { + // Nothing to do, we expect an exception + } + } ).getMessage(); + assertThat( errorMessage ) + .matches( "The INSERT statement for table \\[EntityWithIdentity\\] contains no column, and this is not supported by \\[" + HANAColumnStoreDialect.class.getName() + ", version: [\\d\\.]+\\]" ); } ); }