Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA and enum type

Tags:

java

caching

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

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

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 ?

Thanks

like image 476
Olivier J. Avatar asked Oct 15 '25 15:10

Olivier J.


1 Answers

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. In META-INF/MANIFEST.MH of entity.jar

    classpath: enum.jar

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

like image 122
Aksel Willgert Avatar answered Oct 18 '25 05:10

Aksel Willgert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!