Hibernate throws SQLGrammarException: Error accessing table metadata when @Table is used with schema value and no catalog

Description

If an entity is annotated with @Table and the schema attribute is given a value but the catalog no AND the persistence.xml file does not fill in the hibernate.default_catalog, then this happens:

From my investigations, SQL Server complains because Hibernate issues a query to get table metadata without specifying the database name

Looking in the code, in InformationExtractorJdbcDatabaseMetaDataImpl.java, the problem I think is at line 224:

The code explicitly uses both catalog and schema from the table annotation even though only one might have been specified. (catalog != null

 

schema != null)

Shouldn't it fall back to the current namespace value for the one that is not specified? It doesn't make sense to force the user to specify the database name in persistence.xml or @Table just because the schema was specified. This wasn't required before (worked in 4.3)

Environment

Windows 7, Wildfly 10, Java 8

Activity

Show:
Former user
November 10, 2016, 1:30 PM

I don't know if a closed issue can be reopened?

Chris Cranford
November 28, 2016, 12:53 PM

/ , if this is still a problem, please feel free to open a new issue, reference the test code and feel free to include this JIRA as a related issue.

David Carr
May 3, 2017, 6:59 AM

I wish to advise that I encountered this bug with Red Hat JBoss 7.0.4, which purports to include Hibernate Core version 5.0.11.Final-redhat-1. Can you confirm this has been fixed in 5.0?

For reference: https://forum.hibernate.org/viewtopic.php?p=2491399

Gail Badner
May 26, 2017, 12:13 AM

, could you be running into HHH-11455 instead?

David Carr
May 31, 2017, 2:36 AM

@Gail Badner, no, I don't think it is HHH-11455 for the following reasons:

  • HHH-11455 refers to database vendors that don't support a schema. We are using Microsoft SQL Server 2014 which obviously does utilise schema.

  • I encountered the issue porting our application from JBoss 6.4.5, which integrates Hibernate 4.3, where it previously worked very well.

  • The error trace I received was exactly as given in the description here on HHH-10978, and is how I found this issue and the related StackOverflow thread (https://stackoverflow.com/q/35651997):

  • I could work around the issue by modifying our code so that every instance of a @Table annotation that specified schema also included catalog; or by filling in the default schema and catalog in the persistence.xml configuration file. Neither is a suitable solution for us, however, as we don't want the database name embedded in our code (we have our databases named differently across development/test/production/etc environments). The ultimate fix was to install an updated version of Hibernate (at the time, 5.2.7) into our JBoss 7 platform.

Assignee

Chris Cranford

Reporter

Andrei Serea

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Blocker
Configure