During an upgrade from hibernate 4.3.11 to 5.X I ran into a couple of test failures. I have attached a simplified version of the test.
After some debugging I figured out the following things:
After upgrading to a version >=5, the test not longer runs due to the "schema" not being created.
Even when setting the options:
AvailableSettings.HBM2DDL_AUTO to "update"
AvailableSettings.HBM2DLL_CREATE_SCHEMAS to "true"
the schema does not get created. It also doesn't matter whether or not I use "update" or "create" or "drop-and-create".
I can work around this issue by using: "jdbc:h2:mem:;INIT=create schema IF NOT EXISTS bug" instead of just "jdbc:h2:mem:", however that shouldn't be necessary in my opinion.
I also figured out "why" it works with version 4.3.11. versions < 5 do not check for the schema in the Entity tag. They will simply create the corresponding table in the public schema and be done with it. When there are two Entities with the same table name but different schema their columns get merged into one.
jvm: 1.8 u151
I think this is not a 5.X bug, in fact based on the JPA 2.1 specification if no
is specified then the
have to be used for the database table name.
So based on your example
the following DDL command will be generated
that the db interprets as the creation of a table with name Entity belonging to the bug schema.
So if what you want is the 2 entities be mapped to the same table in the public schema, you can use the following mapping
but if you need to use different schemas then the way is to add the
with the appropriate schema value.
Hi Andrea Boriero,
thanks for your quick reply. After adjusting my code with
, the schemas and entities were properly created for their respective schemas. I expected the schema "bug" and "bug_other" to be created from the @Entity annotations, which didn't happen. I can see why, though.
After your comment I've been skimming through the JPA 2.1 specification and was trying to find the specifics that result in your conclusion.
First I've found that section 11.1.49 Table Annotation says that if a table annotation is missing, it should be assumed that all entries are set to the default values.
Then section 4.3.1 Naming says that
Entities are designated in query strings by their entity names. The entity name is defined by the name element of the Entity annotation (or the entity-name XML descriptor element), and defaults to the unqualified name of the entity class.
So I can see that the entity name is used. Interestingly this conflicts with 2.13 Naming of Database Objects, where it says
This specification requires the following with regard to the interpretation of the names referencing database objects. These names include the names of tables, columns, and other database elements. Such names also include names that result from defaulting (e.g., a table name that is defaulted from an entity name or a column name that is defaulted from a field or property name).
As of yet I have not been able to find something along the lines of "Whenever an entity is mapped to their respective Relational Model, the respective table name is used". I have also not yet found where exactly the specification says how the schema is generated from the @Table annotation, but I can see why it should be like that.
At any rate, I will adjust my code accordingly and this issue can be closed as not valid.