WildFly8 JPA2.1和枚举值

时间:2013-11-14 07:32:42

标签: java jpa jboss wildfly jpa-2.1

我遇到了使用JPA2.1(包含在WildFly8中)的枚举类型的问题。这是使用枚举集的类:

@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();
}

然后我列举了这些动物:

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

现在,当我尝试启动服务器时,我收到以下错误:

[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] 

这适用于Jboss 7.1.1.Final。我应该使用转换器(http://somethoughtsonjava.blogspot.fi/2013/10/jpa-21-type-converter-better-way-to.html)还是可能出现问题?感谢所有帮助!

1 个答案:

答案 0 :(得分:1)

更改

@ElementCollection(targetClass = String.class)

@ElementCollection(targetClass = Animal.class)