Java 如何正确映射集合<;实体>;在使用hibernate的POJO上?

Java 如何正确映射集合<;实体>;在使用hibernate的POJO上?,java,hibernate,set,persistence,Java,Hibernate,Set,Persistence,我正试图与我的用户a类集合绑定,以存储每个用户的配置文件。我弄了点小提琴,现在有点迷路了。myUser类的设置部分当前定义为: @ElementCollection(targetClass = UserProfile.class) @JoinTable(name = "HRM_USER_USER_PROFILE", joinColumns = { @JoinColumn(name = "id_user") }, inverseJoinColumns = { @J

我正试图与我的
用户
a类
集合
绑定,以存储每个用户的配置文件。我弄了点小提琴,现在有点迷路了。my
User
类的设置部分当前定义为:

@ElementCollection(targetClass = UserProfile.class)
@JoinTable(name = "HRM_USER_USER_PROFILE", 
        joinColumns = { @JoinColumn(name = "id_user") },
        inverseJoinColumns = { @JoinColumn(name = "id_profile") })
@Column(name = "id_profile")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Set<String> userProfiles = new HashSet<String>(0);
@Entity
@Table(name="HRM_USER_PROFILE")
public class UserProfile extends BasePojo {

    @Enumerated(EnumType.STRING)
    @Column(name="type", length=15, unique=true, nullable=false)
    private UserProfileType type;

    public UserProfileType getType() {
        return type;
    }

    public void setType(UserProfileType type) {
        this.type = type;
    }
其中UserProfileType是声明为:

public enum UserProfileType {

    USER("USER"),
    DBA("DBA"),
    ADMIN("ADMIN");

    String userProfileType;

    private UserProfileType(String userProfileType){
        this.userProfileType = userProfileType;
    }

    public String getUserProfileType(){
        return userProfileType;
    }
我知道这与注释
@ElementCollection
有关。但是我很困惑

我哪里错了?提前谢谢

试试这个>>>

如果类型为数值,则还可以使用
序数

@Enumerated(EnumType.ORDINAL) //enum type mapping with ordinal Values
@Column(name="type", length=15, unique=true, nullable=false)
private UserProfileType type;

我得到一个
java.lang.Error:未解决的编译问题:类型UserProfile中的方法setType(UserProfileType)不适用于参数(字符串)
。现在将尝试使用
EnumType.ORDINAL
。发布完整的dto。。您发布的代码被拆分。。请按照您的建议进行操作。尝试此操作。我已尝试在UserProfile和UserProfileType的getter上使用
@Enumerated(EnumType.STRING)
注释。不走运。我删除了注释,现在得到了一个
org.hibernate.PropertyAccessException:无法通过b.c.m.h.m.UserProfile.id的反射getter获得字段值
,下面是
java.lang.IllegalArgumentException:java.lang。ClassCastException@...
>试试看
@Enumerated(EnumType.ORDINAL) //enum type mapping with ordinal Values
@Column(name="type", length=15, unique=true, nullable=false)
private UserProfileType type;