Java 如何为hibernate正确映射pojo中的集合?

Java 如何为hibernate正确映射pojo中的集合?,java,spring,hibernate,spring-mvc,mapping,Java,Spring,Hibernate,Spring Mvc,Mapping,我需要在应用程序上对用户进行身份验证,但在用户类上的@ElementCollection映射方面遇到了一些困难 我使用在上找到的示例来构建我的应用程序,所以这里显示的大多数内容与此非常相似 用户类Set最初定义为: @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "HRM_USER_USER_PROFILE", joinColumns = { @JoinColumn(name = "USER_ID") }, i

我需要在应用程序上对用户进行身份验证,但在用户类上的
@ElementCollection
映射方面遇到了一些困难

我使用在上找到的示例来构建我的应用程序,所以这里显示的大多数内容与此非常相似

用户类
Set
最初定义为:

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "HRM_USER_USER_PROFILE", 
     joinColumns = { @JoinColumn(name = "USER_ID") }, 
     inverseJoinColumns = { @JoinColumn(name = "USER_PROFILE_ID") })
private Set<UserProfile> userProfiles = new HashSet<UserProfile>();
// -- getter and setter
UserProfileType
枚举为:

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

String userProfileType;

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

public String getUserProfileType(){
    return userProfileType;
}
已在数据库中持久化,但
用户
类无法读取

编辑-插入
@ElementCollection
部分

@ElementCollection(targetClass = UserProfile.class)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "HRM_USER_USER_PROFILE", 
        joinColumns = { @JoinColumn(name = "id_user") },
        inverseJoinColumns = { @JoinColumn(name = "id_profile") })
@Column(name = "user_profiles")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Set<UserProfile> userProfiles = new HashSet<UserProfile>(0);
@ElementCollection(targetClass=UserProfile.class)
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name=“HRM\U用户\U用户配置文件”,
joinColumns={@JoinColumn(name=“id\u user”)},
inverseJoinColumns={@JoinColumn(name=“id\u profile”)})
@列(name=“user\u profiles”)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
私有集userProfiles=newhashset(0);
我注意到,用户表上的
user\u profiles
列没有创建,也没有任何其他可能引用此映射的表

我使用
@PostConstruct
bean在应用程序启动时加载默认值,而不是运行加载UserProfileType、User及其关系的SQL脚本

我希望打印出完整的用户信息,但我没有得到关于用户配置文件的信息

我错过了什么


提前谢谢

我认为您必须仅在“UserProfile”类上保留“多对多”映射,因为“ElementCollection”用于连接非实体的元素,在您的情况下,必须使用“CollectionTable”注释

@ElementCollection
@CollectionTable(name="HRM_USER_USER_PROFILE", joinColumns=@JoinColumn(name="id_profile"))
@AttributeOverrides({
          @AttributeOverride(name="type", 
                             column=@Column(name="TYPE"))
        })
private Set<UserProfile> userProfiles = new HashSet<UserProfile>(0);
我希望这些信息对你有帮助


祝你好运。

在发布的代码中没有单个元素集合。你在做什么,你期望会发生什么,会发生什么?“我遇到了一些困难”并不能充分描述您的具体问题。对不起,我忘了发布
@ElementCollection
部分。。。正在编辑。为什么您认为必须将ElementCollection添加到此映射?你读过它的javadoc吗?你知道它的用途吗?你想达到什么目的?好吧,我想我可能误读了一些东西。我试图实现的是一个给定用户拥有的所有用户配置文件的映射。
@ElementCollection
@CollectionTable(name="HRM_USER_USER_PROFILE", joinColumns=@JoinColumn(name="id_profile"))
@AttributeOverrides({
          @AttributeOverride(name="type", 
                             column=@Column(name="TYPE"))
        })
private Set<UserProfile> userProfiles = new HashSet<UserProfile>(0);
... 
@AttributeOverrides({
    @AttributeOverride(name="type", column=@Column(name="TYPE")),
    @AttributeOverride(name="otherColumn", column=@Column(name="OTHER_COLUMN"))
})
...