JPA和枚举类型 [英] JPA and enum type

查看:164
本文介绍了JPA和枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JPA实体中使用枚举类型的一个字段:

I use for an JPA entity one field of enum type :

@Enumerated(value=EnumType.STRING)
private Temperament temperament = Temperament.MINEUR_PUR;

我的枚举在我的实体内宣布:

My enum is declared inside my entity :

@Entity
public class Joueur implements Serializable {

.....

    public enum Temperament{
        MINEUR_PUR(30),
        MINEUR(10),
        NEUTRE(0),
        RAIDEUR(-10),
        RAIDEUR_PUR(-30);

        private int temperament_prod_mines;

        private Temperament(int temperament_prod_mines){
            this.temperament_prod_mines = temperament_prod_mines;       
        }

        public int getTemperament_prod_mines() {
            return temperament_prod_mines;
        }

        public void setTemperament_prod_mines(int temperament_prod_mines) {
            this.temperament_prod_mines = temperament_prod_mines;
        }
    }   
}

这是工作,但当我外化我的enum在它自己的文件中,它不再起作用了:

it's work but when I "externalize" my enum in it's own file, it doesn't work anymore :


引起:异常[EclipseLink-7151](Eclipse持久性)服务 - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.ValidationException
异常描述:实体属性[temperament]的类型[class com.sim.basics.enums.Temperament] class [class com.sim.entities.Joueur]不是枚举映射的有效类型。该属性必须定义为Java枚举。

Caused by: Exception [EclipseLink-7151] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException Exception Description: The type [class com.sim.basics.enums.Temperament] for the attribute [temperament] on the entity class [class com.sim.entities.Joueur] is not a valid type for an enumerated mapping. The attribute must be defined as a Java enum.

但它只是一个复制/粘贴......

but it's just a copy/paste...

为什么会出现这种情况?

Why this behaviour ?

谢谢

推荐答案

此问题的一个原因是enum.jar中定义的枚举在运行时未在实体的类路径中找到。

One cause of this problem is that the enumeration defined in enum.jar is in runtime not found on the classpath by the entity.

至少有两种方法可以解决这个问题。

There are atleast two ways to solve this.


  1. 在entity.jar的META-INF / MANIFEST.MH

  1. In META-INF/MANIFEST.MH of entity.jar

classpath:enum.jar

classpath: enum.jar

或者,如果使用maven,则将enum.jar指定为EAR的一部分。

Or to specify the enum.jar as part of the EAR if you use maven.

这篇关于JPA和枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆