休眠映射异常:无法确定类型

Posted

技术标签:

【中文标题】休眠映射异常:无法确定类型【英文标题】:hibernate mapping exception: could not determine type 【发布时间】:2012-11-01 14:34:05 【问题描述】:

我发现了许多关于此类错误的链接,但我没有找到任何解释 Enumtypes 的内容。我下载了最新的hibernate jar文件,但没有用!

错误堆栈跟踪:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [dao-hbm.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: ca.ups.tundra.dao.MessageSystemUserType, at table: History, for columns: [org.hibernate.mapping.Column(SCANMSGSYS)]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.hibernate.MappingException: Could not determine type for: ca.ups.tundra.dao.MessageSystemUserType, at table: History, for columns: [org.hibernate.mapping.Column(SCANMSGSYS)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:276)
    at org.hibernate.mapping.Property.isValid(Property.java:207)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:458)
    at org.hibernate.mapping.SingleTableSubclass.validate(SingleTableSubclass.java:66)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1149)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1334)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
    ... 23 more

以下是我的 EnumUserType 类:

import org.hibernate.usertype.UserType; 

public class EnumUserType<E extends Enum<E>> implements UserType  

扩展 EnumUserType 类:

public class MessageSystemUserType extends EnumUserType<MessageSystem> 

以下是我的枚举类:

public enum MessageSystem  OPSYS, GIPLD, AA, CISCME,LOCAL, TFCS,LH, XPLD, CDC 

在我添加最后两个枚举类型之前,它一直运行良好。

已编辑:这就是我映射枚举的方式,

<property name="messageSystem" type="ca.ups.tundra.dao.MessageSystemUserType" length="10" column="SCANMSGSYS"/>

解决此问题或有关错误的任何建议。

【问题讨论】:

这些枚举是否以某种方式映射到数据库中? @Gregory 这就是我映射它们的方式,&lt;property name="messageSystem" type="ca.ups.tundra.dao.MessageSystemUserType" length="10" column="SCANMSGSYS"/&gt; 【参考方案1】:

检查依赖关系的 web.xml 文件,无论它们是否良好。 检查 .classpath filr 以获取 jar 文件的路径,这可能是一种解决方案。 检查丢失的 jar 文件,这些文件可能在项目的类路径中,但不在您的 lib 中,反之亦然

<classpathentry exported="true" kind="lib" path="../hibernateVersion.jar">
   <attributes>
       <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
   </attributes>
</classpathentry>

【讨论】:

以上是关于休眠映射异常:无法确定类型的主要内容,如果未能解决你的问题,请参考以下文章

将休眠数据类型映射到混合类型存储过程的输出

将 tinyint 映射为布尔休眠

休眠映射异常:实体映射中的重复列

什么 Postgres 类型将映射到休眠中 Float 类型的列?

使用实体框架/休眠与 sql2008 地理数据类型

是否可以使用休眠映射不同的字段类型?