简体   繁体   English

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

[英]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: 我无法使用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)

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. 我将不胜感激。

The column causing the issue is: val levels: SleepLevels . 导致该问题的列是: val levels: SleepLevels

Hibernate doesn't know how to map this class on the database (I assume it's not an enum). Hibernate不知道如何在数据库上映射此类(我认为它不是枚举)。 This means that you need to map it as: 这意味着您需要将其映射为:

  1. Embedded , using the @Embeddaded and @Embeddable annotations; Embedded ,使用@Embeddaded@Embeddable批注;
  2. Association , using the @OneToOne or @ManyToOne annotations. 关联 ,使用@OneToOne@ManyToOne批注。

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. 不知道您的模型的详细信息,我不能为您提供更多帮助,但是您应该能够在官方的休眠文档中找到所需的所有详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM