2012-10-29 30 views
7

Tôi đang sử dụng bản đồ dựa trên hibernate 4 và xml. Dưới đây là ngoại lệ Tôi nhận đượcHibernate EnumType exceptioniation exception

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 

Và đây là bản đồ enum mà tôi đang sử dụng

<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> 

Và đây là enum:

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

đây là thực thể

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 
} 

Tôi đã kiểm tra tutoria l từ đây - here nhưng tôi nhận được lỗi ở trên. Cần giúp đỡ với điều này.

+0

Bạn có biến 'CoachingStatus status' được khai báo trong Thực thể cho tệp' hbm.xml' này –

+0

Có. Tôi cũng sẽ thêm thực thể vào câu hỏi. – Shwetanka

Trả lời

9

Tôi gặp vấn đề tương tự và có vẻ như giải pháp là thêm thông số khác. Bằng cách đó nó sẽ không cố gắng để tồn tại nó như là thứ tự nhưng như chuỗi vì vậy tôi đoán rằng chuỗi hoạt động tốt hơn với loại 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> 

bằng cách thiết lập useNamed để true ngủ đông sẽ lưu trữ enum bằng cách sử dụng tên của nó để dữ liệu của bạn sẽ không phá vỡ nếu bạn thay đổi trật tự trong kiểu enum của bạn.