Hibernate EnumType 实例化异常

2024-05-04

我正在使用 hibernate 4 和基于 xml 的映射。这是我遇到的异常

Caused by: org.hibernate.MappingException: Unable to instantiate custom type: org.hibernate.type.EnumType
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:193)
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:179)
    at org.hibernate.type.TypeFactory.byClass(TypeFactory.java:103)
    at org.hibernate.type.TypeResolver.heuristicType(TypeResolver.java:130)
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:307)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:294)
    at org.hibernate.mapping.Property.isValid(Property.java:238)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:469)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$6.run(AbstractAutowireCapableBeanFactory.java:1504)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1502)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 35 more
Caused by: java.lang.NullPointerException
    at org.hibernate.type.EnumType.setParameterValues(EnumType.java:153)
    at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:131)
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:189)
    ... 53 more

这是我正在使用的枚举映射

<property name="coachingStatus" column="status" update="true" insert="true" index="true">
      <type name="org.hibernate.type.EnumType">
        <param name="enumClass">com.tutorial.enums.CoachingStatus</param>
        <param name="type">12</param>
      </type>
    </property>

这是枚举:

public enum CoachingStatus {
  ACTIVE, BLOCKED, PENDING, EXPIRED
}

这是实体

public class Coaching implements Serializable {
  private Integer id;
  private String name;
  private Long locationId;
  private Integer organisationId;
  private Long ownerId;
  private Date createdOn;
  private Date modifiedOn;
  private CoachingStatus coachingStatus;
  private Long createdBy;
  private Long modifiedBy;
  private String email;
  private String logo;
  private String about;
  private String phone;
... //getters and setters
}

我从这里查看了教程 -here http://blog.javachap.com/index.php/enumeration-mapping-in-hibernate/但我遇到了上述错误。需要帮助。


我遇到了同样的问题,看起来解决方案是添加另一个参数。这样它就不会尝试将其保留为序数而是字符串,因此我猜该字符串与 VARCHAR 类型配合得更好。

 <property name="coachingStatus" column="status" update="true" insert="true" index="true">
    <type name="org.hibernate.type.EnumType">
       <param name="enumClass">com.tutorial.enums.CoachingStatus</param>
       <param name="type">12</param>
       <param name="useNamed">true</param>
  </type>
</property>

通过设置useNamed to truehibernate 将使用枚举的名称来存储枚举,因此如果您更改枚举类型中的顺序,您的数据不会中断。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hibernate EnumType 实例化异常 的相关文章

随机推荐