我无法使用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
}
}
}
另一方面,我尝试从下一个实体中删除属性“级别”,然后,没有异常,并且持久性起作用。因此,我认为此属性正在发生错误。也许我应该添加一个特殊的注释,但是我真的对此感到困惑。
这是我的代码:
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
}
我将不胜感激。
答案 0 :(得分:1)
引起问题的列为:val levels: SleepLevels
。
Hibernate不知道如何在数据库上映射此类(我假设它不是枚举)。 这意味着您需要将其映射为:
@Embeddaded
和@Embeddable
批注; @OneToOne
或@ManyToOne
注释。不知道您的模型的详细信息,我不能为您提供更多帮助,但是您应该能够在官方的休眠文档中找到所需的所有详细信息。