Search code examples
javamongodbkotlinpersistencehibernate-ogm

Exception using Hibernate OGM and MongoDB at Entity declaration


I´m not able to initialize my EntityManagerFactory using Hibernate OGM and mongoDB, I´m getting the following Exception:

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at model.DBManager.initEntityManagerFactory(DBManager.kt:11)
    at controller.restapi.RestapiApplicationKt.main(RestapiApplication.kt:13)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: myPersistenceConfig] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:970)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:895)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at org.hibernate.ogm.jpa.HibernateOgmPersistence.createEntityManagerFactory(HibernateOgmPersistence.java:57)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at model.util.HibernateOGMUtil.<clinit>(HibernateOGMUtil.kt:15)
    ... 2 more
Caused by: org.hibernate.MappingException: Could not determine type for: applicationdatamodel.sleep.SleepLevels, at table: Sleep, for columns: [org.hibernate.mapping.Column(levels)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:456)
    at org.hibernate.mapping.Property.getType(Property.java:69)
    at org.hibernate.jpa.event.spi.JpaIntegrator.integrate(JpaIntegrator.java:150)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:281)
    at org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl.build(OgmSessionFactoryBuilderImpl.java:55)
    at org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl.build(OgmSessionFactoryBuilderImpl.java:23)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)

This is the code involved with the Exception:

class HibernateOGMUtil {

    companion object {

        private lateinit var entityManagerFactory: EntityManagerFactory

        init {
            try{
                entityManagerFactory = Persistence.createEntityManagerFactory("myPersistenceConfig")

            }catch (e:Exception){
                println(System.err.println("Initial EntityManagerFactory creation failed." + e))
            }
        }
        fun getEntityManagerFactory(): EntityManagerFactory{
            return entityManagerFactory
        }
    }
}

On the other hand, I have tried to remove the attribute 'levels' from next Entity and then, there is no exception and persistence works. So I suppose that something wrong is happening with this attribute. Maybe I should add a special annotation but I´m really stuck on this.

Here is my code:

import applicationdatamodel.sleep.SleepLevels
import org.hibernate.annotations.Type
import java.util.*
import javax.persistence.*

@Entity
data class Sleep (
    @Temporal(TemporalType.TIMESTAMP) val dateOfSleep: Date,
    val user_cif: String,
    val duration: Int,
    @Temporal(TemporalType.TIMESTAMP) val startTime: Date,
    @Temporal(TemporalType.TIMESTAMP) val endTime: Date,
    val minutesAsleep: Int,
    val minutesAwake: Int,
    val levels: SleepLevels
){
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Type(type = "objectid")
    private lateinit var id: String
}

I would apreciate any help.


Solution

  • The column causing the issue is: val levels: SleepLevels.

    Hibernate doesn't know how to map this class on the database (I assume it's not an enum). This means that you need to map it as:

    1. Embedded, using the @Embeddaded and @Embeddable annotations;
    2. Association, using the @OneToOne or @ManyToOne annotations.

    Not knowing the details of your model I cannot help you more than this but you should be able to find all the details you need in the official hibernate documentation.