Hibernate:org.hibernate.MappingException:无法确定类型:java.util.Set

时间:2016-05-23 15:42:50

标签: oracle hibernate annotations

我正在尝试在hibernate中使用Set。我不知道如何在Set表上编写注释的问题。

    @Entity
    @Table(name="user_settings")
    public class UserSettings {
    @Id
    @GeneratedValue
    private int id;
    private int userid;
    protected Set<Integer>foreignLanguageId;
    public UserSettings() {   

    }
    public UserSettings(int userid, int nativeLanguageId,
            Set<Integer> foreignLanguageId, Date birthday) {
        this.userid = userid;
        this.nativeLanguageId = nativeLanguageId;
        this.foreignLanguageId = foreignLanguageId;
        this.birthday = birthday;
    }

    @OneToMany
    @JoinColumn(name="userid", nullable=false)// This annotation
    public Set<Integer> getForeignLanguageId() {
        return foreignLanguageId;
    }

错误:

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: user_settings, for columns: [org.hibernate.mapping.Column(foreignLanguageId)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314)...........

1 个答案:

答案 0 :(得分:0)

您应该使用实体来建立关联而不是id

@Entity
@Table(name="user_settings")
public class UserSettings {

    private Set<Language> foreignLanguages;

    @OneToMany
    @JoinColumn(name="fk_user_setting", nullable = false)
    public Set<Language> getForeignLanguages() {
        return foreignLanguages;
    }

}

如果您想使用Integer,可以使用@ElementCollection

@Entity
@Table(name="user_settings")
public class UserSettings {

    private Set<Integer> foreignLanguages;

    @ElementCollection
    public Set<Integer> getForeignLanguages() {
        return foreignLanguages;
    }

}