0

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.

  • Doesn't look like the exception has anything to do with the code you posted? Why don't you post the code where the exception actually happens? – Strelok Aug 23 '18 at 06:24
  • @Strelok I have edited it. You are right! Hope now it´s better. Thank you. – Ángel Rodríguez Aug 23 '18 at 09:41
  • Would you be able to create a small project as an example an publish it somewhere so that we can re-create the error? – Davide D'Alto Aug 23 '18 at 10:00
  • The error might be in the configuration – Davide D'Alto Aug 23 '18 at 10:01
  • You’re not seeing the full exception. Remove your try catch clause so you can get the full stack trace of the hibernate exception. – Strelok Aug 23 '18 at 14:54
  • @Strelok I took your advice. Full stack trace is now updated in my post. My problem should be this: org.hibernate.MappingException: Could not determine type for: applicationdatamodel.sleep.SleepLevels, at table: Sleep, for columns: [org.hibernate.mapping.Column(levels)]. What should I do to solve it? – Ángel Rodríguez Aug 24 '18 at 07:05
  • I solved the problem! I had to add 'Embedded' annotation to 'levels' attribute. Then I added 'Embeddable' annotation to its class. Finally I used this for the rest of embedded classes that represent a embedded object in the mongoDB document. Finally, I had to add 'ElementCollection' annotation to a List of another class that I implemented. – Ángel Rodríguez Aug 25 '18 at 10:23
  • I wrote the answer without noticing your last comment, I guess it will be useful for other people having similar issues. – Davide D'Alto Aug 29 '18 at 08:48

1 Answers1

1

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.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30