7
@Entity
public class TestClass implements Serializable{
    private Integer id;
    private Set<String> mySet;

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    @OneToMany(cascade={CascadeType.ALL})
    public Set<String> getMySet() {
        return mySet;
    }
}

I get the following error.

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: TestClass.mySet[java.lang.String]

or if I leave off the @OneToMany

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: test_class, for columns: [org.hibernate.mapping.Column(my_sets)]

1
  • Hibernate's @CollectionOfElements annotation solved the problem for me. It's hibernate specific, but I don't plan on swapping out my JPA implementation. Commented May 28, 2009 at 18:46

2 Answers 2

8

You'll find a pretty decent answer here. The rules for Lists apply to Sets too.

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

Comments

6

Ooh oh, I had to do this one.

@CollectionOfElements(targetElement = String.class)

2 Comments

This is also what I had to do, but it is hibernate specific. In my situation that's fine with me.
@ElementCollection in JPA2, this answer is the better approach. see stackoverflow.com/questions/287201/…

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.