在实体声明中使用 Hibernate OGM 和 MongoDB 的异常

Exception using Hibernate OGM and MongoDB at Entity declaration

我无法使用 Hibernate OGM 和 mongoDB 初始化我的 EntityManagerFactory,我收到以下异常:

    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)

这是异常涉及的代码:

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
        }
    }
}

另一方面,我尝试从下一个实体中删除属性 'levels' 然后,没有异常并且持久性有效。所以我想这个属性出了点问题。也许我应该添加一个特殊的注释,但我真的坚持这一点。

这是我的代码:

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
}

如果有任何帮助,我将不胜感激。

导致问题的列是:val levels: SleepLevels

Hibernate 不知道如何将此 class 映射到数据库(我假设它不是枚举)。 这意味着您需要将其映射为:

  1. Embedded,使用@Embeddaded@Embeddable注解;
  2. Association,使用 @OneToOne@ManyToOne 注释。

不知道您的模型的详细信息,我无法为您提供更多帮助,但您应该能够在官方休眠文档中找到您需要的所有详细信息。