AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:

Jef*_*eff 1 java enums hibernate javabeans spring-boot

我有一个枚举如下:

public enum UserRole {
    ADMIN,ORGANIZER,USER
}
Run Code Online (Sandbox Code Playgroud)

然后在另一堂课中,我试图收集这个枚举:

@Data
@Entity
public class User {

    @Id
    @GeneratedValue
    Long id;

    @OneToMany
    Collection<UserRole> userRole;

}
Run Code Online (Sandbox Code Playgroud)

但它抱怨以下错误:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.eventer.demo.model.User.userRole[com.eventer.demo.model.UserRole]
Run Code Online (Sandbox Code Playgroud)

小智 5

您不能在非实体类上使用@OneToMany。您应该改为使用@ElementCollection,它可以用于String,Integer,Enum和其他没有主键的基本类型。