我在使用 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)还是有什么问题?感谢所有帮助!