Hibernate EnumType实例化异常 [英] Hibernate EnumType instantiation exception
问题描述
我使用hibernate 4和基于xml的映射。这是我得到的异常
I'm using hibernate 4 and xml based mapping. Here is the exception I'm getting
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
这里是我使用的枚举映射
And here is the enum mapping that I'm using
<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>
这里是枚举:
And here is the enum:
public enum CoachingStatus {
ACTIVE, BLOCKED, PENDING, EXPIRED
}
这里是实体
here is the entity
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
}
我检出了教程从这里 - 这里,但我得到以上错误。需要帮助。
I checked out tutorial from here - here but I'm getting above error. Need help with this.
推荐答案
我有同样的问题,看起来解决方案是添加另一个参数。这样它就不会试图将它作为序号持续存在,而是作为字符串,所以我猜这个字符串在VARCHAR类型中效果更好。
I had same problem and it looks like the solution is to add another parameter. That way it won't try to persist it as ordinal but as string so I guess that string works better with VARCHAR type.
<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
为 true
hibernate将通过使用它的名字来存储enum,所以如果你改变枚举类型的顺序,你的数据不会中断。
by setting useNamed
to true
hibernate will store enum by using it's name so your data will not break if you change order in your enum type.
这篇关于Hibernate EnumType实例化异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!