Search code examples
hibernatemavenliquibasejhipster

liquibase.exception.DatabaseException: java.lang.ClassNotFoundException


I am new to Liquibase and JHipster, and am having some problems with using the maven plugin to generate a diff.

I am getting the following error when running mvn liquibase:diff -e -X

The relevant section of my pom is:

<plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>${liquibase.version}</version>
            <dependencies>
                <dependency>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>3.18.2-GA</version>
                </dependency>
                <dependency>
                    <groupId>org.liquibase.ext</groupId>
                    <artifactId>liquibase-hibernate4</artifactId>
                    <version>${liquibase-hibernate4.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                    <version>${project.parent.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                    <version>5.2.1.Final</version>
                </dependency>

            </dependencies>
            <configuration>
                <changeLogFile>src/main/resources/config/liquibase/master.xml</changeLogFile>
                <diffChangeLogFile>
                    src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml
                </diffChangeLogFile>
                <driver>org.postgresql.Driver</driver>
                <url>jdbc:postgresql://localhost/shiftwork</url>
                <defaultSchemaName/>
                <username>postgres</username>
                <password/>
                <referenceUrl>hibernate:spring:com.teammachine.staffrostering.domain?dialect=org.hibernate.dialect.PostgreSQL94Dialect&amp;hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl>
                <verbose>true</verbose>
                <logging>debug</logging>
            </configuration>
        </plugin>
        <changeLogFile>src/main/resources/config/liquibase/master.xml</changeLogFile>
        <diffChangeLogFile>
            src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml
        </diffChangeLogFile>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://localhost/myApp</url>
        <defaultSchemaName/>
        <username>postgres</username>
        <password/>
        <referenceUrl>hibernate:spring:com.myapp.domain?dialect=&amp;hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl>
        <verbose>true</verbose>
        <logging>debug</logging>
    </configuration>
</plugin>

Perhaps I am wrong, but it is my understanding that the missing class (org.hibernate.boot) is part of Hibernate Core, which is included.

  Number of foreign imports: 1
import: Entry[import  from realm ClassRealm[maven.api, parent: null]]

-----------------------------------------------------

    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:166)
    ... 21 more
Caused by: java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:45)
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:57)
    at liquibase.ext.hibernate.database.HibernateSpringDatabase.buildConfigurationFromScanning(HibernateSpringDatabase.java:243)
    at liquibase.ext.hibernate.database.HibernateSpringDatabase.buildConfiguration(HibernateSpringDatabase.java:55)
    at liquibase.ext.hibernate.database.HibernateDatabase.setConnection(HibernateDatabase.java:45)
    at liquibase.database.DatabaseFactory.findCorrectDatabaseImplementation(DatabaseFactory.java:131)
    at liquibase.database.DatabaseFactory.openDatabase(DatabaseFactory.java:151)
    at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:85)
    at org.liquibase.maven.plugins.LiquibaseDatabaseDiff.performLiquibaseTask(LiquibaseDatabaseDiff.java:166)
    at org.liquibase.maven.plugins.AbstractLiquibaseMojo.execute(AbstractLiquibaseMojo.java:394)
    at org.liquibase.maven.plugins.LiquibaseDatabaseDiff.execute(LiquibaseDatabaseDiff.java:146)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    ... 21 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$Work
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
    ... 33 more
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException
anton@anton-K93SV:~/git/hughestech/shiftwork/staffservice$ 

Solution

  • The issue is that you left the dialect property blank inside the referenceUrl of the liquibase-maven-plugin configuration. The plugin tries to look for that blank class and, of course, fails. This should point to the class implementing the custom dialect to use for Hibernate.

    <referenceUrl>hibernate:spring:com.myapp.domain?dialect=&amp;hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl>
    <!--                                            ^^^^^^^^ -->
    

    If you're using PostgreSQL, it depends on the version of your database. For PostgreSQL 9.4 or later, you can use the class org.hibernate.dialect.PostgreSQL94Dialect as the dialect.

    Therefore, update your configuration to:

    <referenceUrl>hibernate:spring:com.myapp.domain?dialect=org.hibernate.dialect.PostgreSQL94Dialect&amp;hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl>
    

    You're also depending on conflicting versions of Hibernate in your plugin dependencies: liquibase-hibernate4 requires Hibernate 4.3+ but you're depending on Hibernate Core 5.2.1.Final and they are not compatible. As such, you need to remove this dependency on hibernate-core.