【问题标题】:Hibernate: org.hibernate.MappingException: Could not determine type for: java.util.Set休眠:org.hibernate.MappingException:无法确定类型:java.util.Set
【发布时间】:2016-09-20 14:00:25
【问题描述】:

我正在尝试在休眠中使用 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)...........

【问题讨论】:

  • 我认为您应该将 foreignLanguageId 变量的 protected 更改为 private 并与它一起使用 getter 和 setter,以便它可以在 hibernate 中映射。

标签: oracle hibernate annotations


【解决方案1】:

你应该使用实体来建立关联而不是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;
    }

}

【讨论】:

  • 我输入:@ElementCollection,但它仍然无法正常工作,我正在使用 hibernate 4。我现在将尝试创建类语言。谢谢。
  • @LayLeangsros 即使在 Hibernate 3 下也应该可以工作。请在问题中添加@ElementCollection 和堆栈跟踪代码。
猜你喜欢
  • 2011-09-04
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
相关资源
最近更新 更多