Search code examples
javajpajbosswildflyjpa-2.1

WildFly8 JPA2.1 and Enumerated values


I am having problems with enumerated types with JPA2.1 (included in WildFly8). Here is the class using the set of enums:

@Entity
@Table(name = "ZOO")
public class Zoo extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Access(AccessType.PROPERTY)
    private Long id;
    private String name;
    @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
    @CollectionTable(name = "ZOO_ANIMALS", joinColumns
        = @JoinColumn(name = "ZOO_ID", referencedColumnName = "ID"))
    @Enumerated(EnumType.STRING)
    private Set<Animal> animals = new HashSet();
}

Then I have enumeration of the animals:

public enum Animal {
    PANDA,
    LION,
    ZEBRA,
    TIGER;
}

Now when I try to start the server I am getting the following error:

[INFO] [talledLocalContainer] Caused by: org.hibernate.AnnotationException: Attribute [com.project.ejb.model.Zoo.animals] was annotated as enumerated, but its java type is not an enum [java.lang.String]
[INFO] [talledLocalContainer]   at org.hibernate.cfg.annotations.SimpleValueBinder.setType(SimpleValueBinder.java:257)
[INFO] [talledLocalContainer]   at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1370)
[INFO] [talledLocalContainer]   at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:790)
[INFO] [talledLocalContainer]   at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725)
[INFO] [talledLocalContainer]   at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
[INFO] [talledLocalContainer]   at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1675)
[INFO] [talledLocalContainer]   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1408)
[INFO] [talledLocalContainer]   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1824)
[INFO] [talledLocalContainer]   at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:854)
[INFO] [talledLocalContainer]   ... 13 more
[INFO] [talledLocalContainer] 

This worked ok with Jboss 7.1.1.Final. Should I use converter for this (http://somethoughtsonjava.blogspot.fi/2013/10/jpa-21-type-converter-better-way-to.html) or what could be the problem? All help is appreciated!


Solution

  • Change

    @ElementCollection(targetClass = String.class)
    

    to

    @ElementCollection(targetClass = Animal.class)