mongodbhibernatespring-boothibernate-ogm

Hibernate OGM with MongoDB - ClassNotFoundException when creating EntityManagerFactory


I'm trying to use Hibernate OGM with MongoDB but I have a problem with ClassNotFoundException and NoClassDefFoundError when accessing the database and I don't have any idea why.

Here is my code

package com.example.model;

import org.hibernate.annotations.Type;
import org.springframework.stereotype.Indexed;

import javax.persistence.*;

@Indexed
@Entity
@Table(name = "customer")
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
private String id;

private String firstName;
private String lastName;

public Customer() {};

public Customer(String firstName, String lastName) {
    this.setFirstName(firstName);
    this.setLastName(lastName);
}

@Override
public String toString() {
    return String.format(
            "Customer[id=%s, firstName='%s', lastName='%s']",
            getId(), getFirstName(), getLastName());
}

public String getId(){
    return this.id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}
}

And this is where I access the database

package com.example.repository;

import com.example.model.Customer;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;
import java.util.Optional;

@Repository("customerRepository")
public class CustomerNoSQLRepository {

public List<Customer> findAll(){
    EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "ogm-jpa-tutorial" );
    EntityManager entitymanager = emfactory.createEntityManager();

    Query query = entitymanager.createQuery("SELECT * from customer");
    List<Customer> list = query.getResultList();

    for(Customer e:list) {
        System.out.println("Customer NAME :"+e.getFirstName()+" "+e.getLastName());
    }

    return list;
}
}

Here is the dependency in persistence.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.ogm</groupId>
        <artifactId>hibernate-ogm-mongodb</artifactId>
        <version>5.1.0.Final</version>
    </dependency>

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
</dependencies>

This is the persistence.xml

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">

<persistence-unit name="ogm-jpa-tutorial" transaction-type="RESOURCE_LOCAL">
    <!-- Use Hibernate OGM provider: configuration will be transparent -->
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

    <class>test.y.model.Post</class>

    <properties>
        <property name="hibernate.ogm.datastore.provider" value="mongodb" />
        <!--<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />-->
        <property name="hibernate.ogm.datastore.host" value="127.0.0.1"/>
        <property name="hibernate.ogm.datastore.port" value="27017"/>
        <property name="hibernate.ogm.datastore.create_database" value="true"/>
        <property name="hibernate.ogm.datastore.database" value="mongotestdb"/>
        <!--<property name="hibernate.ogm.datastore.username" value="admin"/>-->
        <!--<property name="hibernate.ogm.datastore.password" value="password"/>-->
    </properties>
</persistence-unit>
</persistence>

The error log is in the link here. https://pastebin.com/i1zvqQqq

I think it might be about some missing dependency or dependency version about hibernate so I tried to add some dependencies but still does not work.


Solution

  • The problem here is caused by the mismatch between the Hibernate ORM version would be required by the Hibernate OGM you use and the one you've provided within pom.xml.

    If you want use Hibernate OGM 5.1.x you need to use Hibernate ORM 5.1.x, see the compatibility table here: http://hibernate.org/ogm/releases/5.1.

    <dependency>
    <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.15.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.15.Final</version>
        </dependency>
    

    Or, even better, you could use a newer version of Hibernate OGM.