简体   繁体   中英

Hibernate 5 Schema-validation: missing table with HBM files

This problem is very similar to one I had before. I am converting an existing non-Spring application from Hibernate 3.x to the very latest 5.6.0.Final (at this time). We are using the MariaDB in AWS and are using the latest MariaDB Driver. We are also still using HBM xml files at this time for older tables until we move away from them.

We do have an existing table in the database, I can absolutely confirm this. The error is as follows:

09:32:22.950> ERROR   PersistenceManager:123 Initial SessionFactory creation failed.
org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [my_db.commtemplateinfos]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateTable(AbstractSchemaValidator.java:121)
at org.hibernate.tool.schema.internal.GroupedSchemaValidatorImpl.validateTables(GroupedSchemaValidatorImpl.java:42)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:89)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:200)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:81)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:327)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:728)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:746)
at com.myproject.server.services.db.service.PersistenceManager.buildSessionFactory(PersistenceManager.java:504)

So, this error happens when we try to build the SessionFactory.

I can also provide some of the configuration for this:

<hibernate-configuration>
    <session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.mariadb.jdbc.Driver</property>
    <property name="connection.url">jdbc:mariadb://${mysql.host.name}:3306/${mysql.db.name}?characterEncoding=UTF-8&amp;serverTimezone=UTC</property>
    <property name="connection.username">${mysql.user.name}</property>
    <property name="connection.password"><![CDATA[${mysql.password}]]></property>
    
    <!-- Database Connection Limits -->
    <property name="initialSize">10</property>
    <property name="maxActive">15</property>
    <property name="maxWait">5000</property>
    <property name="maxIdle">1</property>
    <property name="hibernate.default_catalog">${mysql.db.name}</property>
    
    <!-- Naming Strategy -->
    <property name="hibernate.naming.physical-strategy">org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl</property>
    <property name="hibernate.naming.implicit-strategyy">org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl</property>

    <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MariaDB102Dialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="hibernate.transaction.coordinator_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="current_session_context_class">thread</property>

    <!-- Configure the second-level cache  -->
    <property name="hibernate.cache.ehcache.missing_cache_strategy">create</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.jcache.JCacheRegionFactory</property>
    <property name="hibernate.javax.cache.provider">org.ehcache.jsr107.EhcacheCachingProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">${hibernate.show.sql}</property>

    <!-- Update the database schema on startup -->
    <property name="hbm2ddl.auto">validate</property>

    <!-- Configure the connection Pool for Hibernate -->
    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

... mappings here ...
<mapping resource="com/myproject/server/model/Project.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

I tried the naming strategy based on some other posts here, but they didn't do anything.

Here is the definition of this 'list' within the Project.hbm.xml:

<list name="commTemplateInfos" table="commtemplateinfos" cascade="all, delete-orphan" lazy="false">
    <key column="projectId" not-null="false" />
    <list-index column="listIndex" />
    <composite-element class="com.myproject.server.model.CommTemplateInfo" >
        <property name="name"   not-null="false" />
        <property name="frequency"   not-null="false" />
    </composite-element>
</list>

I think I've provided everything I need to provide. I have tried a lot of things based on similar posts here on StackOverflow, and no luck. The worst part is that when I run my unit tests I do not get this error locally. It only happens when I run this against our TeamCity which uses another database. It is still a MariaDB there, and yes, the table really does actually exist in that database, so no, that's not the issue.

I am sure this is a minor configuration issue from the legacy HBM xml files. I honestly cannot wait until we go to proper annotated classes. In the meantime, there has got to be a simple solution to fix this.

Well, I now know what happened, and this has to do with some craziness in Hibernate 5 and database table names. The actual table name in the databases was: commTemplateInfos.

When I ran my unit test locally against my local database (MariaDB), it was NOT case-sensitive for the table names. However, our database that TC uses IS very much case-sensitive and as a result would not find the table at all.

The solution was pretty simple, rename the table to: comm_template_info So, we space out with an underscore (_) but also made it singular to match all the other table names. Once we did this, our hbm.xml file was changed to match this exact table name and our problem went away.

I hope this helps someone else out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM