javahibernatehibernate-ogm

org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.ogm.service.impl.OgmConfigurationService]


I've created an application that implements hibernate OGM with MongoDB. It runs perfectly fine inside Eclipse however, when I build a fat jar and attempt to run it I get the following error:

Exception in thread "main" org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.ogm.service.impl.OgmConfigurationService]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
        at org.hibernate.ogm.boot.impl.OgmMetadataBuilderInitializer.contribute(OgmMetadataBuilderInitializer.java:28)
        at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:141)
        at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:136)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:218)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:174)
        at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:171)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:119)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:61)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:50)
        at org.hibernate.ogm.jpa.HibernateOgmPersistence.createEntityManagerFactory(HibernateOgmPersistence.java:57)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at com.craftingdead.masterserver.player.PlayerManager.<init>(PlayerManager.java:76)
        at com.craftingdead.masterserver.MasterServer.<init>(MasterServer.java:55)
        at com.craftingdead.masterserver.MasterServer.main(MasterServer.java:70)

As far as I'm concerned I have all the correct dependencies in the classpath. I'm using JDK 11 and compiled the fat jar using https://github.com/johnrengelman/shadow.

Here's my persistence.xml:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
    <persistence-unit name="crafting-dead-master-server">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <properties>
            <property name="hibernate.ogm.datastore.provider"
                value="MONGODB" />
            <property name="hibernate.ogm.datastore.database"
                value="crafting-dead-master-server" />
            <property name="hibernate.ogm.datastore.create_database"
                value="true" />
            <property name="hibernate.search.default.directory_provider"
                value="filesystem" />
            <property name="hibernate.search.default.indexBase"
                value="lucene/indexes" />
        </properties>
    </persistence-unit>
</persistence>

...and my build.gradle:

plugins {
    id 'java-library'
    id 'com.github.johnrengelman.shadow' version '5.1.0'
}

sourceCompatibility = targetCompatibility = 1.11

repositories {
    jcenter()
    maven { 
        name = 'jitpack'
        url = 'https://jitpack.io'
    }
    maven {
        name = 'minecraft'
        url = 'https://libraries.minecraft.net'
    }
}

sourceSets {
    main {
        java {
            srcDir "network/src/main/java"
        }
    }
}

jar {
    manifest {
        attributes(
            'Main-Class': 'com.craftingdead.masterserver.MasterServer',
            'Multi-Release': 'true'
        )
    }

    from {
        configurations.required.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

shadowJar {
   exclude "META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat"
}

configurations {
    api.extendsFrom required
}

dependencies {
    // Project Lombok
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.8'
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.8'

    // Netty/All In One
    required group: 'io.netty', name: 'netty-all', version: '4.1.36.Final'

    // Guava: Google Core Libraries For Java
    required group: 'com.google.guava', name: 'guava', version: '27.1-jre'

    // Apache Log4j Core
    required group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.2'

    // Apache Log4j JUL Adapter
    required group: 'org.apache.logging.log4j', name: 'log4j-jul', version: '2.11.2'

    // Apache Log4j SLF4J Binding 
    required group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.11.2'

    // Fastutil
    required group: 'it.unimi.dsi', name: 'fastutil', version: '8.2.2'

    // EventBus
    required group: 'org.greenrobot', name: 'eventbus', version: '3.1.1'

    // Brigadier
    required group: 'com.mojang', name: 'brigadier', version: '1.0.17'

    // Apache Commons Lang
    required group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'

    // Authlib
    required group: 'com.mojang', name: 'authlib', version: '1.5.25'

    // SnakeYAML
    required group: 'org.yaml', name: 'snakeyaml', version: '1.24'

    // Hibernate OGM For MongoDB
    required group: 'org.hibernate.ogm', name: 'hibernate-ogm-mongodb', version: '5.4.1.Final'

    // JAXB API
    required group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'

    // Hibernate Search ORM
    required group: 'org.hibernate', name: 'hibernate-search-orm', version: '5.10.4.Final'

    // Jackson Databind
    required group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.0.pr1'   
}

Solution

  • This was resolved by moving away from using a 'fat jar' and keeping dependencies in their respective jars. Jar merging was causing complications with service resolution, thus producing an UnknownServiceException.