如何创建枚举类型的映射?

How to create a map to an enum type?

我不想使用 @Type 注释,因为它是特定于休眠的,并且未在 JPA 2.0 规范中指定。

我的数据库中有 integer 类型的 type_id 列,它可能只包含 2 个不同的整数值:1, 2。我创建了枚举类型:

public enum ExternalTransactionType {

     TO_LOTO(1), WITHDRAW(2);

    private int value;

    ExternalTransactionType(int value) {
        this.value = value;
    }

    public String getName() {
        return name();
    }

    public int getValue() {
        return value;
    }

    public static ExternalTransactionType getByValue(int value) {
        for (ExternalTransactionType type : ExternalTransactionType.values()) {
            if (value == type.getValue()) {
                return type;
            }
        }
        throw new IllegalArgumentException("Illegal value: " + value);
    }
}

并尝试将 属性 添加到映射中,如下所示:

@Column(name="type_id")
private Integer typeId;

public ExternalTransactionType getType() {
    return ExternalTransactionType.getByValue(typeId);
}

public void setType(ExternalTransactionType type) {
    this.typeId = type.getValue();
}

但是当我尝试添加像

这样的限制时
criteria.add(Restrictions.eq("typeId", ExternalTransactionType.TO_LOTO))

抛出 ClassCastException。怎么了?

java.lang.ClassCastException: ua.com.winforce.loto_partner.commons.db.entity.ExternalTransactionType cannot be cast to java.lang.Integer
    org.hibernate.type.IntegerType.set(IntegerType.java:41)
    org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
    org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
    org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1698)
    org.hibernate.loader.Loader.bindParameterValues(Loader.java:1669)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1554)
    org.hibernate.loader.Loader.doQuery(Loader.java:661)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    org.hibernate.loader.Loader.doList(Loader.java:2211)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
    org.hibernate.loader.Loader.list(Loader.java:2090)
    org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
    org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    ua.com.winforce.loto_partner.commons.db.dao.PlayersExternalTransactionDAOImpl.getNewRealPlayersCount(PlayersExternalTransactionDAOImpl.java:55)
    ua.com.winforce.loto_partner.admin.db.service.ReportServiceImpl.getPartnerReport(ReportServiceImpl.java:627)
    ua.com.winforce.loto_partner.admin.db.service.ReportServiceImpl.getPartnerReport(ReportServiceImpl.java:302)
    ua.com.winforce.loto_partner.admin.db.service.ReportServiceImpl.getPartnerReport(ReportServiceImpl.java:309)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    com.sun.proxy.$Proxy117.getPartnerReport(Unknown Source)
    ua.com.winforce.loto_partner.admin.ui.panels.reports.PartnersReportController.doSearch(PartnersReportController.java:121)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:387)
    org.ajax4jsf.component.AjaxActionComponent.broadcast(AjaxActionComponent.java:55)
    org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:324)
    org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:299)
    org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:256)
    org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:469)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
    org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
    org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    org.springframework.security.ui.SessionFixationProtectionFilter.doFilterHttp(SessionFixationProtectionFilter.java:67)
    org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)
    org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
    org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:277)
    org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
    org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
    org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    ua.com.winforce.loto_partner.admin.filter.EncodingFilter.doFilter(EncodingFilter.java:14)

传递整数值而不是枚举,因为 type_id 的类型是 Integer 而不是 Enum 例如

 criteria.add(Restrictions.eq("typeId", ExternalTransactionType.TO_LOTO.getValue()))