0

I am trying to use Set in hibernate. The problem I don't know to how to write annotation on the Set table.

    @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;
    }

Error:

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
  • I think you should change protected to private for your foreignLanguageId variable and use getters and setters along with it, so it can be mapped in hibernate. Commented May 23, 2016 at 16:03

1 Answer 1

0

You should to use entity to make an association not 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;
    }

}

if you want to use Integer you can use @ElementCollection

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

    private Set<Integer> foreignLanguages;

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

I put: @ElementCollection, but it is still not working, I am using hibernate 4. I will try to create class Language now. Thx.
@LayLeangsros It should work even with Hibernate 3. Please, add to the question your code with @ElementCollection and a stack trace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.