java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Hibernate of current version doesn't match h2

Open castleKing1997 opened this issue 4 years ago • 2 comments

I use hibernate and h2 inherited from the parent pom.xml whose version are 5.2.18.Final. But I find the org.hibernate.dialect.H2Dialect extends from org.hibernate.dialect.Dialect didn't override method getCurrentSchemaCommand, which leeds it reture null. Then I received an exception when I use hibernate to connect h2database:

org.hibernate.HibernateException: Use of DefaultSchemaNameResolver requires Dialect to provide the proper SQL statement/command but provided Dialect [org.hibernate.dialect.H2Dialect] did not return anything from Dialect#getCurrentSchemaCommand
	at org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver$SchemaNameResolverFallbackDelegate.resolveSchemaName(DefaultSchemaNameResolver.java:100)
	at org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver.resolveSchemaName(DefaultSchemaNameResolver.java:76)
......

It occurs becaus

public String resolveSchemaName(Connection connection, Dialect dialect) throws SQLException {
	final String command = dialect.getCurrentSchemaCommand();
	if ( command == null ) {
		throw new HibernateException(
				"Use of DefaultSchemaNameResolver requires Dialect to provide the " +
						"proper SQL statement/command but provided Dialect [" +
						dialect.getClass().getName() + "] did not return anything " +
						"from Dialect#getCurrentSchemaCommand"
		);
	}
        ......
}

but the code can run successfully for some reasons. you can reproduce this case in my code Is is necessary to fix it? Maybe a new version of hibernate?

castleKing1997 avatar Dec 13 '21 02:12 castleKing1997

Hi, I'd like to work on this issue. Could it be assigned to me if still available?

Linly1080 avatar Apr 23 '22 16:04 Linly1080

Thanks @Linly1080, please go ahead

iluwatar avatar Sep 10 '22 12:09 iluwatar

@iluwatar sorry, I have no time to do the work now.

Linly1080 avatar Sep 17 '22 03:09 Linly1080

The issue is still present in 5.4.24. To verify, run com.iluwatar.metamapping.AppTest and look for

org.hibernate.HibernateException: Use of DefaultSchemaNameResolver requires Dialect to provide the proper SQL statement/command but provided Dialect [org.hibernate.dialect.H2Dialect] did not return anything from Dialect#getCurrentSchemaCommand
	at org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver$SchemaNameResolverFallbackDelegate.resolveSchemaName(DefaultSchemaNameResolver.java:100)
	at org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver.resolveSchemaName(DefaultSchemaNameResolver.java:76)

robertvolkmann avatar Oct 24 '22 16:10 robertvolkmann

We would get rid of the HibernateException by implementing a custom SchemaNameResolver

By default, Hibernate uses the [org.hibernate.dialect.Dialect#getSchemaNameResolver](https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/dialect/Dialect.html#getSchemaNameResolver--). You can customize how the schema name is resolved by providing a custom implementation of the [SchemaNameResolver](https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/engine/jdbc/env/spi/SchemaNameResolver.html) interface.

https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#_table_qualifying_options

robertvolkmann avatar Oct 24 '22 17:10 robertvolkmann

I advise closing this issue because it is only a strange error message from some weird internal Hibernate implementation. You will see a similar exception for all MySQLDialects because MySQL doesn't support schemas.

robertvolkmann avatar Oct 24 '22 18:10 robertvolkmann