0

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!

drodil
  • 2,292
  • 1
  • 22
  • 37

1 Answers1

1

Change

@ElementCollection(targetClass = String.class)

to

@ElementCollection(targetClass = Animal.class)
Sandhu Santhakumar
  • 1,678
  • 1
  • 13
  • 24
  • Oh sorry, misspelled that but it is not resolving the problem. Thanks anyways, fixed it to the question :) – drodil Nov 14 '13 at 07:37
  • Great, that worked. I wonder why I have specified to String.class in the first place.. – drodil Nov 14 '13 at 10:11